[英]Reloading or Recreating old activity after changing a Setting Android
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. 如果没有,请向用户发送Wifi设置。
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. 也许最简单的解决方案是调用Activity
类的recreate()
方法。 Something like the following: YourActivity.recreate()
. 类似于以下内容: YourActivity.recreate()
。 Keep in mind that this only works for >= API 11. 请记住,这仅适用于> = API 11。
You can also use an Intent
to do a simple relaunch of your current Activity
as follows: 您还可以使用Intent
来重新启动当前的Activity
,如下所示:
Intent intent = getIntent();
finish();
startActivity(intent);
You can find more information here: How do I restart an Android Activity 您可以在此处找到更多信息: 如何重新启动Android活动
在代码的onResume
部分中,我添加了一个if statement
,用于再次检查互联网。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.