【问题标题】:OnActivityResult called prematurely with Settings menuOnActivityResult 使用设置菜单过早调用
【发布时间】:2012-09-25 16:11:51
【问题描述】:

在我的应用程序中,在进入应用程序之前,我必须检查我的 wifi 连接,如果 wifi 未启用,则将用户带到 wifi 设置。

我不想使用WifiManager.setWifiEnabled();,因为我想给用户设置它的机会。

我已经引用了链接,onActivityResult() called prematurely

还有onActivityResult and the Settings Menu

但这对我不起作用。 OnActivityResult()onResume() 几乎在我进入“设置”菜单的同时被调用。

这是我的代码,

    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setMessage("You are not currently connected to any network. Please turn on the network connection to continue.")
    alert.setPositiveButton("Settings", new DialogInterface.OnClickListener() 
    {
        @Override
        public void onClick(DialogInterface dialog, int arg1) 
        {
            Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
            startActivityForResult(intent,SETTINGSCODE);
            dialog.cancel();
        }
    });
    alert.show();

在 onActivityResult() 中,我再次检查网络状态,这是在更改设置之前调用的。

我该怎么做才能OnActivityResult()

只有从settings menu回来后才会被调用?

请帮我解决这个问题..!!

【问题讨论】:

    标签: android android-intent android-activity application-settings android-lifecycle


    【解决方案1】:

    这就是我解决这个问题的方法。

    参考 answer 发布的类似问题,我将 startActivityForResult() 更改为 startActivity(),因为操作调用的顺序类似于

    1. startActivityForResult()
    2. onActivityResult()
    3. onCreate()(新活动)
    4. setResult() 或 finish()

    该控件被带到设置页面,用户可以在该页面打开/关闭 Wi-Fi,然后再返回应用程序。 :)

    【讨论】:

      猜你喜欢
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      • 2015-12-04
      • 2013-05-28
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      相关资源
      最近更新 更多