2013-10-17 31 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”

相关问题