-1

In my app when I click on a menu button it will go to an activity where I will load data and images from a remote server to a custom list view. The problem is if the WiFi is switched off, I'm getting a crash in the second activity. I have put the code in the try catch block and set the adapter to null in the catch section.So when i click the button it will go the activity and if the WiFi is off, since i set adapter = null in the catch activity will be shown with out a list view. How can i stay in the menu screen with out getting a crash. Any idea?

        Intent mainIntent = new Intent( SplashScreenActivity.this, GiftListActivity.class );
        SplashScreenActivity.this.startActivity( mainIntent );

This is my call to the second activity.

try{
    adapter = new GiftJewelryListAdapter(GiftListActivity.this, giftlist);
    itemsListView.setAdapter(adapter);
}
catch(Exception e){
    adapter=null;
    MyAlertDialog.ShowAlertDialog(GiftJewelryListActivity.this, "", "Check Network Connection", "OK");
}

I want to stay in the activity from which i called, after showing the message "check network connection".What change should i do here?

kristianp
  • 5,496
  • 37
  • 56
playmaker420
  • 1,527
  • 4
  • 27
  • 52

3 Answers3

2

You can check network availability in the activity from which you called the new activity. If a network is available, you can start the new activity.

The below function is used to detect whether device is connected to a network or not.

 public static boolean isNetworkAvailable(Context context) {
     ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
           for (int i = 0; i < info.length; i++) {
              if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                 return true;
              }
           }
        }
     }
     return false;
  }

You can invoke the function as shown below...

if(!isNetworkAvailable(context))
     MyAlertDialog.ShowAlertDialog(GiftJewelryListActivity.this, "", "Check Network Connection", "OK")
else{
     Intent mainIntent = new Intent( SplashScreenActivity.this, GiftListActivity.class );
     SplashScreenActivity.this.startActivity( mainIntent );
}

Dont forget to add permission in Manifest.XMl .

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

Hope it helps..

Rahmathullah M
  • 2,676
  • 2
  • 27
  • 44
1

I think instead of using the try catch block, check whether wifi connection is available before calling the second activity. Only if connection is available, call the second activity. This way you can stay in the current activity. See this link which explains about checking internet connection.

Community
  • 1
  • 1
Renjith
  • 5,783
  • 9
  • 31
  • 42
1

One way is to detect when the user hits the OK button of your warning dialog box by defining a DialogInterface.OnClickListener in which you call YourActivity.this.finish() to close the activity and get back to the previous activity.

ovmjm
  • 1,624
  • 12
  • 15