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();
}
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 Intent
to 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
在代码的onResume
部分中,我添加了一个if statement
,用于再次检查互联网。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.