[英]Set root activity/intent in Android
I'm switching between different Activities using Intents. 我正在使用Intents在不同的活动之间切换。 For one Activity, I would like it to clear the history stack so when the user presses the back button, it takes them Home instead of previous activities in my application.
对于一个Activity,我希望它清除历史堆栈,因此当用户按下后退按钮时,它会将它们作为Home而不是我的应用程序中的先前活动。
I had to implement the same thing for my project. 我必须为我的项目实现同样的事情。 What I ended up doing was to replace: startActivity(i);
我最终做的是替换:startActivity(i); with startActivityForResult(i, UniqueId);
with startActivityForResult(i,UniqueId); in all the activities I wanted to be part of the "history stack".
在我希望成为“历史堆栈”一部分的所有活动中。
Then implemented: 然后实施:
setResult(UniqueId);
finish();
in the child activity when I wanted to close the child and return to "home/root". 在孩子活动中我想要关闭孩子并返回“home / root”。
In the parent activity, I implemented: 在父活动中,我实施了:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == UniqueId && resultCode == UniqueId) {
setResult(UniqueId);
finish();
}
super.onActivityResult(requestCode, resultCode, data);
}
Which effectively means if the child activity sent a "home/root" result (represented by UniqueId), all parents of that child activity that have used "startActivityForResult(i, UniqueId);" 这实际上意味着如果子活动发送了“home / root”结果(由UniqueId表示),该子活动的所有父母都使用了“startActivityForResult(i,UniqueId);” will close as well.
也会关闭。
Hope that makes sense? 希望有道理吗?
You could try calling finish()
on the activities you don't want to keep around when calling startActivity
? 您可以尝试在调用
startActivity
时不想保留的活动上调用finish()
吗?
Or, the less android solution, listen for onBackPressed()
in your activities and then call startActivity()
on your main activity with the CLEAR_STACK
flag set. 或者,较少的android解决方案,在您的活动中侦听
onBackPressed()
,然后在设置了CLEAR_STACK
标志的主活动上调用startActivity()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.