Android从后台删除活动

Emr*_*s90 63 stack android

好的,所以我有点难以理解这件事.所以我有MainActivity,从那里可以启动一个Activity到DegreePlanActivity,从那里可以启动另一个Activity到EditDegreePlan.我在AndroidManifest中将EditDegreePlan设置为noHistory.问题是在他们保存EditDegreePlan后它会向DegreePlan启动一个Activity.因此,如果用户按下Back,则必须按两次才能再次进入MainActivity.我想摆脱它,所以他们只需要按一次.我很难过如何做到这一点.

如果我将DegreePlanActivity设置为noHistory,那么在EditDegreePlan中他们无法按回到它.

我已经尝试重写onBackPressed方法并启动对MainActivity的意图.那么问题是他们必须多次按Back才能退出应用程序.

我该怎么办?

Ara*_* GM 129

FLAG_ACTIVITY_CLEAR_TOP清除您的活动堆栈,您可以使用以下代码:

Intent intent = new Intent(this, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

请记住,这标志清零只是中级活动,例如,如果你有A,B,C中的Back Stack,然后从C活性打算d使用此标志这个明确Back Stack和堆栈将A,B,C,d但如果你从活动D转到活动A,带有此标志,B,C,D活动将从堆栈中弹出,您将在后堆栈中只有A.


Kam*_*kar 13

API> = 15到API 23用户活动名称的简单解决方案.

 Intent nextScreen = new Intent(currentActivity.this, MainActivity.class);
 nextScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
 startActivity(nextScreen);
 ActivityCompat.finishAffinity(currentActivity.this);
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢,但是不建议使用IntentCompat.FLAG_ACTIVITY_CLEAR_TASK,请改用Intent.FLAG_ACTIVITY_CLEAR_TASK (3认同)

小智 11

我建议您在启动EditDegreePlan-Activity时使用startActivityForResult()而不是简单地startActivity(),如Android教程中所述.

在EditDegreePlan-Activity中,然后调用

setResult(RESULT_OK);
finish();
Run Code Online (Sandbox Code Playgroud)

如果您不希望EditDegreePlan-Activity中有任何数据,那么您不一定要实现onActivityResult.


Isa*_*tte 10

要从清单android:noHistory="true"内的返回堆栈中删除活动,请添加到清单文件内的活动中。

请参阅下面的示例。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.activity"
      android:versionCode="1"
      android:versionName="1.0">
 <application android:name="MyApp" android:label="My Application">
    <activity android:name=".LoginActivity" 
      android:noHistory="true"> //add this line to your activity inside manifest
     <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
   </activity>
 </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)


Hen*_*nry 6

看来,如果你根本没有指定任何标志,你将获得所需的行为.后退按钮会做正确的事情.要在代码中关闭活动,请使用finish()与用户按后退按钮相同的方法.因此,当您完成EditDegreePlan时,您将自动到达DegreePlan,无需任何调用Intents.


liv*_*ove 6

您可以在开始新活动之前调用完成。这将从堆栈中删除当前活动,因此当您从下一个活动按下后退按钮时,不会从堆栈历史记录中调用第一个活动。

Intent i = new Intent(MainActivity.this, NextActivity.class);
finish();  
startActivity(i);
Run Code Online (Sandbox Code Playgroud)


Vik*_*tel 5

这是你的流程:

MainActivity - > DegreePlanActivty - > EditDegreePlan - > DegreePlan - > MainActivty

在"DegreePlan"中覆盖这些方法

public void onBackPressed() {
   super.onBackPressed();
   Intent goToMainActivity = new Intent(getApplicationContext(), MainActivity.class);
   goToMainActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Will clear out your activity history stack till now
   startActivity(goToMainActivity);
}
Run Code Online (Sandbox Code Playgroud)