Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

437
Views
KILL App's MainActivity on Startup if(networkState==false || null)

Good day friends of mine.

I want to add a functionality to my ANDROID application, which I think it is new to me... but it is new in the development world. The application runs very well at the moment... But I need to add this functionality as explained below.

Please see the sample picture below...which tells exactly what I want to do.

I want to check network connection state on Application Startup. If connection state is true, then, the application should load the MainActivity.xml. If connection state is false, then, the application should return a new String ("Error") message.

Please see my code sample below...in the onCreate() {} thread. NOTE: I am not proficient in android development please.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        activeNetwork = cm.getActiveNetworkInfo();//this is line 91...
        Log.d("Network Info", "" + activeNetwork);//log the connection info

        if (activeNetwork != null && activeNetwork.isConnected()) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            //initialize the status bar textview control
            statusBar = (TextView) findViewById(R.id.textViewStatusMsg);//status bar of the UI
            titleLogin = (TextView) findViewById(R.id.textViewLogin);//title text of the UI

If the above condition is TRUE, the application runs well but if the following condition below is FALSE, the application crashes with ANDROID's error message "Unfortunately, App has crashed!". Instead of it giving me its own message and crashing, I want to kill the app programmatically using the else...statement below.

} else {
            data = new String("NO NETWORK CONNECTION! TRY AGAIN...");
            return; //return the message data and kill the APP's UI here
        }
    }

Any help will suffice please... I am not well vested in android programming and development...

enter image description here

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I guess the reason for crashing is because of misplaced of super.onCreate method.

Move this statement outside if block and either you can do a toast message or show an alert dialog.

If above solution didn't work, comment the crash stacktrace.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!