凡片段时保存其状态FragmentTransaction.Replace? [英] Where Fragment save its State when FragmentTransaction.Replace?

本文介绍了凡片段时保存其状态FragmentTransaction.Replace?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

研究背景:我有不同的滚动内容在右边,左边的菜单,和。我想保存的滚动位置。但我失败了。然后...

我已经建立了一个非常简单的项目进行测试。

I have set up a very simple project to test it.

在的话,我在左边不同的内容全部持有℃的菜单,和;的EditText> 在右侧。 (相同的类,但是,他们是不同的实例的)

In words, i have a menu on the left, and different contents all holding an <EditText> on the right. (of the same class though, they are of different instances)

在codeS,(很简单)

的content.xml

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Please save my text ..." />

ContentFragment.java

package ... import ...

public class ContentFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.content, container, false);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        System.out.println("Why this is NOT called ?");
        Log.v("onSaveInstanceState()", "outState = " + outState);
    }
}

和生成的程序的 MenuFragment.java

package ... import ...

public class MenuFragment extends ListFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, new String[]{"Item 1", "Item 2", "Item 3"}));
    }
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        ((MainActivity) getActivity()).click();
    }
}

最后,我们的 MainActivity.java

package ... import ...

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null)
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.menu, new MenuFragment()).commit();
    }
    public void click() {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content, new ContentFragment()  // everytime load a new instance
                ).addToBackStack(null).commit();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        System.out.println("... and this is NOT called neither :-/");
        Log.v("onSaveInstanceState()", "outState = " + outState);
    }
}

要进行测试,请不旋转你的设备。

To test it, please DO NOT rotate your device.

首先,从菜单中选择一个项目,然后键入的东西在文本框中。然后选择另一个项目,并再次输入一些东西。重复几次。然后再返回和...

First, choose an item from the menu, then type something in the textbox. Then choose another item and type something again. Repeat for a few times. Then navigate back and ...

你会发现你的文本previously键入 ARE!救了

you will find your texts previously typed ARE SAVED !!!

这是不奇怪的文本保存...

It is not surprised that the texts are saved ...

但它的的惊讶的文本保存的所有的的onSaveInstanceState()函数没有被调用。

But it is surprised the texts are saved with all the onSaveInstanceState() functions NOT called.

在做Android的救他们,而替换 FragmentTransaction

When did Android save them while replaced by FragmentTransaction?

如果我想另外一些数据保存到每个片段实例(如滚动位置),在那里我应该把我的codeS?

If i want to additionally save some data into each Fragment instance (like the scroll position), where should i put my codes?

推荐答案

在默认情况下,EDITTEXT挽救他们自己的实例。见雅拉T.的答案。

By default, Edittext save their own instance. See Yalla T.'s answer.

<一个href="http://stackoverflow.com/questions/12420396/how-to-retain-edittext-data-on-orientation-change">How保持对方向变化的EditText数据?

这篇关于凡片段时保存其状态FragmentTransaction.Replace?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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