如何重新启动或恢复活动

时间:2014-03-31 14:03:53

标签: java android

我想在禁用连接时更改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网络时,我如何重新启动或恢复应用程序以检查网络连接?

由于

5 个答案:

答案 0 :(得分:1)

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

startActivity(getIntent());
finish();

这将解决问题

答案 1 :(得分:0)

使用onRestart方法

protected void onRestart() {

    super.onRestart();
}

答案 2 :(得分:0)

从活动

调用recreate()
 public void recreate ()

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

答案 3 :(得分:0)

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

从WirelessSettings返回活动时,调用onResume方法并为status var分配一个新值。

答案 4 :(得分:0)

 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