Android的 - addToBackStack()不工作? [英] Android - addToBackStack() doesn't work?

查看:128
本文介绍了Android的 - addToBackStack()不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是负责将片段回栈的功能:

This is the function responsible for adding fragments to back stack:

public void populateContent(File f)
{

    ContentFragment cf = new ContentFragment(ctx, ac, this);
    FragmentTransaction transaction = ac.getSupportFragmentManager().beginTransaction();;
    cf.updateView(f);

    transaction.replace(R.id.contentFragment, cf);

    transaction.addToBackStack(null);

    transaction.commit();

}

当我点击后退按钮,最后一个片段中没​​有加载(没有任何反应)。

When I click the back button, the last fragment doesn't get loaded (nothing happens).

任何想法可能会导致什么呢?

Any idea what might be causing this?

编辑:FragmentManager日志

FragmentManager log.

http://pastebin.com/mYnVdkLG

在我看来,如果我的申请被保存了第二个观点的两倍,而不是挽救第一个,然后第二个观点。

It seems to me as if my application is saving the second view twice, instead of saving the first one and then the second view.

推荐答案

我不知道实际的解决方案,但我可以指导你一下,也许你能弄清楚的问题是什么。

I am not sure about the actual solution but I can guide you a bit and perhaps you can figure out what the problem is.

您确实更换两个片段?如果你不那么有没有交易恢复。也就是你的第一个片段从XML加入?管理者不知道这个片段,你可能需要添加的第一个片段使用事务了。

Are you actually replacing two fragments? If you do not then there is no transaction to revert. Also is your first Fragment added from XML? The manager will not know about this fragment and you might need to add the first Fragment using a transaction too.

注意检查如果(savedInstanceState == NULL)performFirstTransaction(),否则你最终会增加你的第一个片段键两次。

Be careful to check for if (savedInstanceState == null) performFirstTransaction() otherwise you will end up adding your first Fragment twice.

一个很好的想法是使用 enableDebugLogging FragmentManager 。这会告诉你哪些片段经理知道。

One good idea is to use enableDebugLogging in the FragmentManager. This will tell you about which fragments the manager knows about.

请参阅此: <一href="http://developer.android.com/reference/android/app/FragmentManager.html#enableDebugLogging(boolean)">http://developer.android.com/reference/android/app/FragmentManager.html#enableDebugLogging(boolean)

作为一个侧面说明,它不建议使用自定义构造函数的片段。这是因为,如果你的应用程序就会被杀死,并重新实例化的操作系统,它会调用空构造。

As a side note, it is NOT recommended to use a custom constructor for your Fragment. That is because if your app gets killed and re-instantiated by the OS it will call the empty constructor.

您应该使用一个静态方法,如 ContentFragment.getInstance(小于PARAMS&GT;)来创建片段

You should use a static method such as ContentFragment.getInstance(<params>) to create your Fragment.

请参阅的更多信息:<一href="http://developer.android.com/reference/android/app/Fragment.html">http://developer.android.com/reference/android/app/Fragment.html在类概述部分。

See more info at: http://developer.android.com/reference/android/app/Fragment.html at the "Class Overview" section.

我希望我的回答可以帮助你一点点地找到问题所在。

I hope my answer helps you a little bit to find the problem.

这篇关于Android的 - addToBackStack()不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆