Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

427
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda