Android remove Activity from back stack

2019-01-21 18:15发布

Okay so I'm kind of stumped on what to do with this. So I have the MainActivity, and from there an Activity can be launched to DegreePlanActivity, and from there another Activity can be launched to EditDegreePlan. I've got EditDegreePlan set to noHistory in the AndroidManifest. The problem is after they save the EditDegreePlan it launches an Activity to DegreePlan. So if the user presses Back they have to press it twice to get to MainActivity again. I want to get rid of that so they only have to press it once. I'm stumped on how to do this though.

If I set DegreePlanActivity to noHistory then they couldn't press Back to it while in EditDegreePlan.

I've tried overriding onBackPressed method and launching an intent to MainActivity. The problem then is that they have to press Back multiple times to exit the app then.

What should I do?

标签: android stack
7条回答
神经病院院长
2楼-- · 2019-01-21 18:58

I would suggest that you use startActivityForResult(), instead of simply startActivity(), when you launch the EditDegreePlan-Activity, as described in the Android tutorials.

In the EditDegreePlan-Activity you then call

setResult(RESULT_OK);
finish();

If you don't expect any data from the EditDegreePlan-Activity, then you don't necessarily have to implement the onActivityResult.

查看更多
放荡不羁爱自由
3楼-- · 2019-01-21 19:03

simple solution for API >= 15 to API 23 user activity name in intent.

 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);
查看更多
叼着烟拽天下
4楼-- · 2019-01-21 19:05

You can call finish before you start your new activity. This will unload the current activity, so when you press back button on then next activity, the first activity will not be there anymore.

Intent i = new Intent(MainActivity.this, NextActivity.class);
finish();  
startActivity(i);
查看更多
Root(大扎)
5楼-- · 2019-01-21 19:10

Here is your flow:

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

Override these method inside your "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);
}
查看更多
霸刀☆藐视天下
6楼-- · 2019-01-21 19:11

It seems, that you will get the desired behavior if you do not specify any flags at all. The back button would just do the right thing. To get an activity closed from within your code use the finish() method it has the same effect as the user pressing the back button. So you will automatically arrive at DegreePlan when you finish the EditDegreePlan, no need to call any Intents either.

查看更多
狗以群分
7楼-- · 2019-01-21 19:15

FLAG_ACTIVITY_CLEAR_TOP clears your Activity stack , you can use the code below:

Intent intent = new Intent(this, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Remember that this flag clears just Intermediate Activities , for example if you have A,B,C in your Back Stack then going from C Activity to D with this flag this does not clear Back Stack and the Stack would be A,B,C,D but if you go from Activity D to Activity A with this flag , B,C,D Activities will pop up from the stack and you will have just A in the Back Stack .

查看更多
登录 后发表回答