最好的办法,以两个片段之间切换 [英] Best way to switch between two fragments

查看:242
本文介绍了最好的办法,以两个片段之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感兴趣的有两个片段之间切换单个活动的最佳途径。

I'm interested in the best way to have a single activity that switches between two fragments.

我可能读15堆栈溢出的职位,并就如何做到这一点5博客帖子,和,虽然我觉得我拼凑一个解决方案,我不相信这是最好的之一。所以,我想听听市民的意见就来处理这个正确的方式,特别是关于父活动和片段的生命周期。

I've read probably 15 Stack Overflow posts and 5 blogs posts on how to do this, and, while I think I cobbled together a solution, I'm not convinced it's the best one. So, I want to hear people's opinions on the right way to handle this, especially with regards to the lifecycle of the parent activity and the fragments.

下面是详细情况:


  1. ,可以同时显示两种可能的片段之一的父活动。

  2. 的两个片段有,我想在整个会话持续下去,但不一定需要会话之间持久化状态。

  3. 其他的一些活动,比如父活动,碎片可能会淹没在后面堆栈和摧毁,由于内存不足。

  4. 我想使用后退按钮片段之间移动(所以,按照我的理解,我不能用setRetainInstance)的能力。

除了一般建筑的建议,我有以下未决问题:

In addition to general architecture advice, I have the following outstanding questions:


  1. 如果父活动被销毁由于内存不足,我怎么保证两个片段的状态将被保留,按照这个帖子:<一href=\"http://stackoverflow.com/questions/8482606/when-a-fragment-is-replaced-and-put-in-the-back-stack-or-removed-does-it-stay\">When一个片段被换下,换上中后卫栈(或删除)它留在记忆?。我只是需要一个指向父活动的每个片段?

  2. 什么是父活动来跟踪它目前正在显示该片段?
  3. 的最佳方式
  1. If the parent activity is destroyed due to low memory, how do I guarantee that the states of both fragments will be retained, as per this post: When a Fragment is replaced and put in the back stack (or removed) does it stay in memory?. Do I just need a pointer to each fragment in the parent activity?
  2. What is the best way for the parent activity to keep track of which fragment it is currently displaying?

在此先感谢!

推荐答案

我最终都将使用支持片段经理的片段,然后用分离/附加在它们之间进行切换。我能够在onResume使用commitAllowingStateLoss(),因为我保留看法的状态别处,并手动设置正确的片段()。

I ended up adding both of the fragments using the support fragment manager and then using detach/attach to switch between them. I was able to use commitAllowingStateLoss() because I retain the state of the view elsewhere, and manually set the correct fragment in onResume().

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.my_layout, new AFragment(), TAG_A);
    fragmentTransaction.add(R.id.my_layout, new BFragment(), TAG_B);
    fragmentTransaction.commit();
}

public void onResume() {
    super.onResume();
    if (this.shouldShowA) {
        switchToA();
    } else {
        switchToB();
    }
}

private void switchToA() {
    AFragment fragA = (AFragment) getSupportFragmentManager().findFragmentByTag(TAG_A);
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.detach(getSupportFragmentManager().findFragmentByTag(TAG_B));
    fragmentTransaction.attach(fragA);
    fragmentTransaction.addToBackStack(null);

    fragmentTransaction.commitAllowingStateLoss();
    getSupportFragmentManager().executePendingTransactions();
}

这篇关于最好的办法,以两个片段之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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