2017-06-03 46 views
1

我用於使用onBackPressed()..如何在進入主屏幕後重新啓動應用程序?

@Override 

      public void onBackPressed() 
     { 
     final AlertDialog.Builder alert = new AlertDialog.Builder(HomeActivity.this); 
     alert.setMessage("Are you sure want to exit?"); 
     alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       moveTaskToBack(true); 
      } 
     }); 
     alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       alert.setCancelable(true); 
      } 
     }); 
     alert.create().show(); 
} 

我回家屏幕上的成功,但是當我再次打開應用程序,應用程序在恢復state.So打開後,如何再次重新啓動應用程序破壞活動代碼?

+0

添加完成(),或使用意圖標誌CLEAR_TASK和NEW_TASK –

+0

你能給我完整的代碼Divyesh? –

+0

odkhe 6 k奈? –

回答

0

試試這個:

  Intent intent = new Intent(Intent.ACTION_MAIN); 
      intent.addCategory(Intent.CATEGORY_HOME); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(intent); 
      finish(); 

,或者其他的解決方案:

  Intent intent = new Intent(MainActivity.this, SplashActivity.class); 
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 
      intent.putExtra("EXIT", true); 
      startActivity(intent); 

,並在閃屏活動:

@Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getIntent().getBooleanExtra("EXIT", false)) { 
      finish(); 
      return; 
     } 
..... 
+0

Thanx Divyesh ... –

相關問題