1

I have an activity which displays data and before everything starts I do a check whether there is internet or not. If there isn't I send the user to the Wifi settings.

The problem I am facing is that after the user does connect to the internet and presses back, the app is empty without the data. Is there a way to reload this activity or recreate it?

I tried this code but it recreates the activity before I go to the settings so the dialogue box stays open

@Override
protected void onResume() {
super.onResume();
this.onCreate(null);
}

This is the code that checks the internet connection

    if(!isNetworkAvailable()){
         //Toast.makeText(getApplicationContext(), "internet is not avialable", Toast.LENGTH_LONG).show();

        AlertDialog.Builder ad = new AlertDialog.Builder(this);
        ad.setMessage("There is no Internet Connection.\n Please check your settings.");
        ad.setCancelable(false);
        ad.setPositiveButton("Internet Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) 
            {
                startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));

            }               

        });
        ad.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) 
            {
                finish();
            }
        });

        ad.show();
    }
freeman292
  • 47
  • 4

2 Answers2

1

Perhaps the simplest solution is to call the recreate() method of the Activity class. Something like the following: YourActivity.recreate(). Keep in mind that this only works for >= API 11.

You can also use an Intentto do a simple relaunch of your current Activity as follows:

Intent intent = getIntent();
finish();
startActivity(intent);

You can find more information here: How do I restart an Android Activity

Community
  • 1
  • 1
Willis
  • 5,308
  • 3
  • 32
  • 61
  • but when should I do this. I need to do this after the user connects to the internet. @Willis – freeman292 Apr 21 '15 at 19:04
  • @freeman292, You should do this after the user connects to the internet then., possibly by setting a flag and doing it in `onResume()`. – Jonas Czech Apr 21 '15 at 19:11
  • I would notify the user at the beginning whether or not they are connected to the internet. If they are, then no problem. If not, simply keep rechecking the connection status until the connect and then, when they are successfully connected, relaunch the application. – Willis Apr 21 '15 at 19:12
  • @Willis I do that. You can see in the code I have posted in my question. The question is where should I put the bit of code that relaunches the application?. after the part that connects to the internet? – freeman292 Apr 21 '15 at 19:43
0

in the onResume part of the code I added an if statement that checks the internet again.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
freeman292
  • 47
  • 4