片段呼吁片段上的屏幕旋转松动状态 [英] Fragment calling fragments loosing state on screen rotation

查看:140
本文介绍了片段呼吁片段上的屏幕旋转松动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我创建了一个默认的抽屉式导航活动的项目。
所以我有一个MainActivity与被替换为菜单上的每个项目一个片段。

Hi i created a project with a default "Navigation Drawer Activity". So i have a MainActivity with a fragment with is replaced for each item on menu.

一个菜单是客户以显示客户的列表。

One of the menus is "Customers" with shows a list of customers.

从客户的片段,我可以看到这个客户的利益,有一个片段(CustomerListFragment)调用的利益(InterestsListFragment)。

From customers fragment i can see the Interests of this customers, with is a Fragment(CustomerListFragment) calling the interests(InterestsListFragment).

有更水平,但要短这就足够了。

There is even more levels, but to be short that's enough.

这是对MainActivity的code,我用它来从片段调用片段以及

This is the code on MainActivity that i use to call fragment from fragment and pass data between

public void passData(Object[] data, Fragment f) {
    Bundle args = new Bundle();
    args.putSerializable("PASSED_DATA", data);
    f.setArguments(args);
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, f)
            .addToBackStack("")
            .commit();
}

和我使用类似:

mCallbacks.passData(new Object[]{c}, new OpportunityListFragment());

问题是,当我旋转手机不从活动至极水平无论我有,这又回到了所谓的(CustomerListFragment)的第一个片段,如果我点击返回的手机它会回到我当我旋转手机。

The problem is that when i rotate the phone does not matter from wich level of activity i have, it comes back to the first fragment called(CustomerListFragment), and if i click "Back" on cellphone it gets back to where i was when i rotate the phone.

我有什么做的,为了避免这样的问题?为什么它会回到第一个活动诱发如果我更换片段?

What do i have to do, to avoid this kind of problem? why it gets back to the first activity evoked if i am replacing fragments?

推荐答案

从STE福答案是正确的,但我们的编程探索。这是一个很好的工作$ C $谷歌文档在C @ 处理运行时更改。有迹象表明,你需要做的2 code片段。

The answer from ste-fu is correct but let's explore programmatically. There is a good working code in Google documentation @ Handling Runtime Changes. There are 2 code snippets that you have to do.

1)code片断:

public class MyActivity extends Activity {

private RetainedFragment dataFragment;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // find the retained fragment on activity restarts
    FragmentManager fm = getFragmentManager();
    dataFragment = (DataFragment) fm.findFragmentByTag("data");

    // create the fragment and data the first time
    if (dataFragment == null) {

请注意:code使用的 FragmentManager 以查找当前片段。如果片段是null,则UI或应用还没有被执行。如果不为空,那么你就可以得到数据的 RetainedFragment 的对象。

Note: Code uses FragmentManager to find the current Fragment. If fragment is null, then the UI or app has not been executed. if not null, then you can get data from RetainedFragment object.

2)需要保留片段的状态。

2) Need to retain the Fragment state.

public class RetainedFragment extends Fragment {

// data object we want to retain
private MyDataObject data;

// this method is only called once for this fragment
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // retain this fragment
    setRetainInstance(true);
}

请注意: setRetainInstance在OnCreate中使用。并推荐子类片段,将其命名为 RetainedFragment ,上段1。使用

Note: setRetainInstance is used in OnCreate. And subclassing the Fragment is recommended, naming it RetainedFragment, used on snippet 1.

这篇关于片段呼吁片段上的屏幕旋转松动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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