最多的Android导航与多家长的活动 [英] Android up navigation for an Activity with multiple parents
问题描述
我对执行向上导航在一个应用程序使用此导航树一个问题:
该标准实施后退按钮是好的。
努力实现向上按钮时,问题开始。
我的期望:
- 当用户在详细5活动和preSS应用程序转到了按钮的列表3活动
- 当用户在详细7活动和preSS向上按钮的应用程序可追溯到首页活动
因此,在不同的方面,我想对后面的堆栈这种行为:
Android的文件(实施祖传导航)建议使用下列code办理向上导航
意图parentActivityIntent =新的意图(这一点,MyParentActivity.class);
parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(parentActivityIntent);
完();
但由于详细活动的父活动的不同,不同的导航路径上,我不知道哪一个它确实是。所以我不能把它的意图。
有没有办法知道在Android回栈?
真正的父活动如果没有,有没有办法实现这个程序正确的向上导航
我会用我的保罗的回答评论贴
这个想法是有最后的父的堆栈
活动走过。例如:
公共静态堆栈<类<>>父母=新的堆栈<类<>>();
现在你所有的家长活动(被认为家长-eg你的案件的活动:列表和家庭),您添加到其的onCreate
:
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
parents.push(的getClass());
//或者更好parents.push(getIntent());作为@jpardogo指出
//当然,改变对方codeS利用保存的意向。
// ...您的code休息
}
当您想返回到父
的活动,您可以使用以下(根据您的code):
意图parentActivityIntent =新的意图(这一点,parents.pop());
parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(parentActivityIntent);
完();
我希望是正确的(:
I have a problem for implementing up navigation on an app with this navigation tree:
The standard implementation of the back button is fine.
The problem start when trying to implement the Up button.
What I expect:
- when the user is on Detail 5 Activity and press the up button the app goes to List 3 Activity
- when the user is on Detail 7 Activity and press the up button the app goes back to Home Activity
So in different terms, I'd like to have this behaviour on the back stack:
The Android documentation (Implementing Ancestral Navigation) advice to use the following code to handle up navigation:
Intent parentActivityIntent = new Intent(this, MyParentActivity.class);
parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(parentActivityIntent);
finish();
But because the parent activity of the Detail Activity differs on the different navigation path I don't know which one it really is. So I can't call it in the Intent.
Is there a way to know the real parent activity in the Android back stack?
If not, is there a way to implement a correct up navigation in this app?
I will stick with my comment on Paul's answer:
The idea is to have a Stack of the last Parent
Activities traversed. Example:
public static Stack<Class<?>> parents = new Stack<Class<?>>();
Now in all your parent activities (the activities that are considered parents -e.g. in your case: List and Home), you add this to their onCreate
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
parents.push(getClass());
//or better yet parents.push(getIntent()); as @jpardogo pointed
//of course change the other codes to make use of the Intent saved.
//... rest of your code
}
When you want to return to the Parent
activity, you can use the following (according to your code):
Intent parentActivityIntent = new Intent(this, parents.pop());
parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(parentActivityIntent);
finish();
I hope am right (:
这篇关于最多的Android导航与多家长的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!