【问题标题】:How to restart or resume activity如何重新开始或恢复活动
【发布时间】:2014-03-31 22:21:35
【问题描述】:

如果连接被禁用,我想在更改 wifi 设置菜单时重新启动活动。 这是我的代码(onCreate 方法):

        boolean status = ConnectionManager.getConnectivityStatusString(this);

    if(status){
        networkStatus.setText("SEI CONNESSO AD INTERNET !");
    }else{
        networkStatus.setText("connection not present");
        FragmentManager fragmentManager = getSupportFragmentManager();
        MsgAlertConnection newAlertConnection = new MsgAlertConnection();
        newAlertConnection.show(fragmentManager, "Fragment");
    }

这是显示对话框的代码:

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    // 1. Instantiate an AlertDialog.Builder with its constructor
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // 2. Chain together various setter methods to set the dialog characteristics
    builder.setTitle("Connection Error !")
           .setMessage("Please check your Internet Connection for start the application.")
           .setPositiveButton("Back to Settings", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   Intent intent=new Intent();
                   intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.WirelessSettings"));
                   startActivity(intent);
               }
           });
    // Create the AlertDialog object and return it
    return builder.create();
}

当用户将 wifi 网络更改为“开启”时,我如何重新启动或恢复应用程序以检查网络连接?

谢谢

【问题讨论】:

    标签: java android


    【解决方案1】:

    我认为如果您需要重新启动活动 - 那么您的设计有问题 - 无论如何

    startActivity(getIntent());
    finish();
    

    这样就可以了

    【讨论】:

    • onCreateDialog 方法在我在 onCreate 方法中调用的另一个类中。如果连接被禁用,对话框会将用户发送到 wifi 设置并打开 wifi 连接。用户返回应用程序时如何重新检查连接?
    • 您可以在将用户发送到网络设置时引发一个标志,并在该标志打开时检查您的 OnResume() 中的连接
    • 感谢我通过使用布尔值来解决它。如果用户进入设置页面,则为 TRUE,如果按下取消按钮,则为 FALSE。
    【解决方案2】:

    带有onRestart方法

    protected void onRestart() {
    
        super.onRestart();
    }
    

    【讨论】:

      【解决方案3】:

      从活动中调用 recreate()

       public void recreate ()
      

      使用新实例重新创建此 Activity。这导致与由于配置更改而创建 Activity 时的流程基本相同——当前实例将经历其生命周期到 onDestroy() 并在其后创建一个新实例。

      【讨论】:

        【解决方案4】:

        尝试将第一段代码放在 onResume() 方法中。

        当从 WirelessSettings 返回到活动时,调用 onResume 方法并为状态变量分配一个新值。

        【讨论】:

          【解决方案5】:
           Try this one.
          
           @Override
            protected void onResume() {
              super.onResume();
              notify("onResume");
            }
          
          or
          
          @Override
          protected void onStart() {
              super.onStart();  // Always call the superclass method first
          
              // The activity is either being restarted or started for the first time
              // so this is where we should make sure that GPS is enabled
              LocationManager locationManager = 
                      (LocationManager) getSystemService(Context.LOCATION_SERVICE);
              boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
          
              if (!gpsEnabled) {
                  // Create a dialog here that requests the user to enable GPS, and use an intent
                  // with the android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS action
                  // to take the user to the Settings screen to enable GPS when they click "OK"
              }
          }
          
          @Override
          protected void onRestart() {
              super.onRestart();  // Always call the superclass method first
          
              // Activity being restarted from stopped state    
          }
          

          请访问此链接。 http://developer.android.com/training/basics/activity-lifecycle/stopping.html

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-05-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多