繁体   English   中英

更改设置Android后重新加载或重新创建旧活动

[英]Reloading or Recreating old activity after changing a Setting Android

我有一个显示数据的活动,在一切开始之前,我要检查是否有互联网。 如果没有,请向用户发送Wifi设置。

我面临的问题是,用户确实连接到互联网并按回去之后,该应用程序为空,没有数据。 有没有办法重新加载此活动或重新创建它?

我尝试了这段代码,但是在进入设置之前它会重新创建活动,因此对话框保持打开状态

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

这是检查互联网连接的代码

    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();
    }

也许最简单的解决方案是调用Activity类的recreate()方法。 类似于以下内容: YourActivity.recreate() 请记住,这仅适用于> = API 11。

您还可以使用Intent来重新启动当前的Activity ,如下所示:

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

您可以在此处找到更多信息: 如何重新启动Android活动

在代码的onResume部分中,我添加了一个if statement ,用于再次检查互联网。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM