1. Are you ready for the Galaxy S20? Here is everything we know so far!

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

Discussion in 'Android Development' started by Eyo Honesty, Apr 29, 2017.

  1. Eyo Honesty

    Eyo Honesty Newbie
    Thread Starter

    Good day friends of mine.

    I want to add a functionality to my 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...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 here...in the onCreate() {} thread.
    NOTE: I am not proficient in android development please.

    Code (Text):
    1.  
    2. @Override
    3. protected void onCreate(Bundle savedInstanceState) {
    4.     cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    5.     activeNetwork = cm.getActiveNetworkInfo();//this is line 91...
    6.     Log.d("Network Info", "" + activeNetwork);//log the connection info
    7.  
    8.     if (activeNetwork != null && activeNetwork.isConnected()) {
    9.         super.onCreate(savedInstanceState);
    10.         setContentView(R.layout.activity_main);
    11.  
    12.         //initialize the status bar textview control
    13.         statusBar = (TextView) findViewById(R.id.textViewStatusMsg);
    14.         titleLogin = (TextView) findViewById(R.id.textViewLogin);//title text of this UI
    15.  
    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 "Sorry, 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.
    Code (Text):
    1.  
    2.     } else {
    3.         data = new String("NO NETWORK CONNECTION! TRY AGAIN...");
    4.         return; //return the message data and kill the APP's UI here
    5.     }
    6. }
    7.  
    Any help will suffice please... I am not well vested in android programming and development...
     

    Attached Files:



  2. Best Answer:
    Post #2 by bayk, Apr 30, 2017 (1 points)

    1. Download the Forums for Android™ app!


      Download

       
  3. bayk

    bayk Member

    You should call methods
    super.onCreate(...);
    setContentView(...);

    for your else case too, it crash because you didn't init your activity properly.

    Also it is not fine to do such long operations in onCreate. You have to create you main activity first, then check network for a long time.
    [​IMG]
    In your case I would go with 3 activities. It is heavy architecture but it is easiest to implement.
    1. Main Activity with layout that said 'Please wait for network checking'. This activity does network checking in background and start activity #2 if network works, or #1 if doesn't
    2. Network OK layout and activity
    3. Network not found error activity.
     
  4. Eyo Honesty

    Eyo Honesty Newbie
    Thread Starter

    @bayk, thanks for your suggestions.

    Meanwhile, in the ErrorActivity, I would to create to an alert dialog to display the message overlapping the MainActivity.
    That is, the MainActivity does not close but, the ErrorActivity opens on the MainActivity as a dialogBox with the error message.

    How so I instantiate this ErrorActivity so that, it appears on the main window, just as you see in the picture above?

    Thank you.
     
Loading...

Share This Page

Loading...