2013-10-17 32 views
0

我正在導航活動2,在按下後退按鈕時,它應該退出或結束應用程序而不顯示主要活動。我正在使用ViewPager(Activity2),如果我僅在ViewPager的第一頁中導航,那麼我在導航其他頁面時,代碼正在backpress中工作,退出或完成應用程序的代碼不起作用並顯示mainActivity。完成或退出次要活動的應用程序?

MainActivity

Intent myIntent = new Intent(MainActivity.this, Activity2.class); 

活性2

@Override 
public void onBackPressed() { 
     super.onBackPressed(); 
    this.finish(); 
    android.os.Process.killProcess(android.os.Process.myPid()); 
    System.exit(0); 
    getParent().finish(); 
    Activity a1 = (Activity)this.getBaseContext(); 

    if(this.getParent()!=null){ 
     Activity a = (Activity)this.getParent().getApplicationContext(); 
     a.finish(); 
    } 

    Log.i("Backpressed", "pressed"); 

} 
+0

您不應該使用'System.exit(0)'或退出應用程序。點擊返回按鈕應該採取以前的活動。 使用操作欄點擊應用程序圖標導航到主屏幕。點擊返回按鈕退出應用程序 – Raghunandan

+0

我不確定,如果它被調用。如果你不能幫助改善我的問題,請不要低估。 – rahstame

+0

我din't downvote,我不明白「我不知道,如果它」 – Raghunandan

回答

2

這會工作下,

Intent intent = new Intent(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_HOME); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intent); 
+1

在您的代碼上執行此代碼'BackButton'的佈局以及'onBackPressed()'這是覆蓋的方法..它肯定會工作.. :) –

+0

謝謝。它更好,而不是調用finish(); –

0

你確定之後 「System.exit(0);」被調用,其餘的代碼會被調用?我記得,它不會。它應該像你想要的那樣工作。

無論如何,請不要使用任何這些方法。在某些情況下,System.exit可能足以滿足您的需求,但實際上它殺死了應用程序的所有線程,並認爲這是一種骯髒的方式。而是從Activity 1調用startActivityForResult到Activity 2。然後,在活動2中調用finish並將結果設置爲something,然後在activity 1上檢查結果是否要退出該應用程序,如果是,請在那裏調用finish()。

0

嘗試添加該android:noHistory="true"到您的清單這樣

<activity 
      android:noHistory="true" 
      android:name="com.example.activities.MainPageActivity" 
      android:configChanges="orientation|keyboardHidden|screenSize|screenLayout" 
      android:label="@string/app_name" 
      android:theme="@android:style/Theme.NoTitleBar" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
0

在MainActivity添加finish();
您還可以添加以下行您的清單MainActivity

 android:excludeFromRecents="true" 
     android:finishOnTaskLaunch="true" 
     android:noHistory="true" 
0

,如果您使用的是backpresses殺process..then殺死所有的process..use一個for循環得到PID爲所有processes..and殺them..like這

ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 
    List<ActivityManager.RunningAppProcessInfo> list = am 
      .getRunningAppProcesses(); 


    if (list != null) { 
     for (int i = 0; i < list.size(); ++i) { 
      String processid = list.get(i).processName; 


      String packageName = mContext.getPackageName(); 
      if (processid.contains(packageName + ":homescreen")) { 
       //here homescreen is the name you given in manifest for android:process 

       Process.killProcess(list.get(i).pid); 

      } 

和清單爲每個活動添加機器人:過程= 「:anyname」 LIK e此

android:process =「:homescreen」

相關問題