问题描述
目前Android应用程序的结构。
Currently the structure of the android app is
Home
About us
Products
product1
product2
product3
Contact us
由于有一个侧面菜单,我可以直接访问该产品1,我现在的尝试是添加到backstack的每一笔交易,它有一个行为是很奇怪的。
As there is a side-menu and I can access the product 1 directly, My current attempt is add to backstack for every transaction,and it has a behavior that is quite strange
如果我进入这样的:
Home->product1->About us
流是我进入主页后,我点击侧边菜单中的产品1,侧面菜单上输入产品1页上点击关于我们之后
The flow is after I enter the home page, I click on the product1 on the side menu, after enter product1 page click on the about us on the side menu
1时preSS后退按钮,它就会回到产品1,但它应该去家里
1st time press back button, it will go back to product1, but it should go to home
第二次preSS后退按钮,它会去家,但它应该去的产品页
2nd time press back button, it will go to home, but it should go to the Products page
如何处理在这样的情况下的backstack?感谢您的帮助。
How to handle the backstack in such situation? Thanks for helping.
推荐答案
您会需要很聪明,当你在适当的改变碎片和 popBackStack
倍,以控制堆栈。下面是从我的应用程序之一(另处理堆栈中重用现有片段)的例子:
You're going to need to be smart when you are changing the fragments and popBackStack
at the appropriate times in order to control the stack. Here's an example from one of my applications (also handles reusing existing fragments in the stack):
// Idea from http://stackoverflow.com/questions/18305945/how-to-resume-fragment-from-backstack-if-exists
private void setPrimaryContentFragment(BaseFragment fragment, boolean allowStack){
final String backStackName = fragment.getBackStackName();
final FragmentManager manager = getSupportFragmentManager();
final boolean fragmentPopped = manager.popBackStackImmediate(backStackName, 0);
if (!fragmentPopped) { //fragment not in back stack, create it.
if (!allowStack && manager.getBackStackEntryCount() > 1) {
manager.popBackStack(manager.getBackStackEntryAt(0).getId(), 0);
}
final FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.primary_content, fragment);
transaction.addToBackStack(backStackName);
transaction.commit();
try {
manager.executePendingTransactions();
} catch (IllegalStateException exception) {
// Move along as this will be resolved async
}
}
}
的 getBackStack()
方法是在我的基地片段实现,具有的默认实现:
The getBackStack()
method is implemented in my base fragment and has a default implementation of:
public String getBackStackName() {
return getClass().getName();
}
的 allowStack
标志被用来控制是否可以在backstack多个条目。
The allowStack
flag is used to control whether there can be more than one entry in the backstack.
至于当用户直接导航到你可能需要做那一个细节页面中插入产品片段。即:执行对产品取代,然后一个替换产品的详细信息。
希望这code片段和链接的帖子将帮助你拿出你需要的解决方案。
As far as inserting the Product fragment when a user navigates directly to a detail page you'll likely need to do just that. ie: Execute a replace for Products and then a replace for Product details.Hopefully this code snippet and the linked post will help you come up with the solution you need.
这篇关于管理在Android上backstack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!