片段会自动保存和恢复EditTexts的状态 [英] Fragment automatically saving and restoring state of EditTexts

查看:277
本文介绍了片段会自动保存和恢复EditTexts的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个似乎是自动恢复的状态在一个屏幕旋转配置更改片段。我可以在每当屏幕旋转时onCreatView被称为片段日志验证。尽管打电话到applyDefaults(),即当屏幕旋转和onCreateView()被调用取得了用户保留项草案。

我的理解是,我将不得不保存状态的onSaveInstanceState中的()和它onCreateView()恢复,但似乎并不如此。有人能解释一下吗?

  @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
    Log.d(TAGonCreateView());
    查看查看= inflater.inflate(R.layout.fragment_email_entry,集装箱,FALSE);    m_hostNameEditText =(EditText上)view.findViewById(R.id.hostNameEditText);
    //为其他领域设置参考    applyDefaultsForNewEmailAccount();
    返回视图。
}私人无效applyDefaults(){
    m_hostNameEditText.setText();
    //设置其他默认设置
}

这可能与我的活动从SingleFragmentActivity继承的事实做,所以也许它看到该片段已在视图中。然而,我们知道,Fragment.onCreateView()被调用。

 公共抽象类SingleFragmentActivity延伸活动{    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.single_fragment_activity);
        FragmentManager fragmentManager = getFragmentManager();
        片段片段= fragmentManager.findFragmentById(R.id.single_frame_container);
        如果(片段== NULL){
            片段= createFragment();
            fragmentManager.beginTransaction()
                    。新增(R.id.single_frame_container,片段)
                    。承诺();
        }
    }    公共抽象片段createFragment();
}


解决方案

  

就像一个活动,一个片段会自动保存数据
  在捆绑任何片段View组件由视图组件的id。
  就如同在活动,如果你实现
  的onSaveInstanceState()方法,确保您添加呼叫到
  super.onSaveInstanceState()方法,以便保持该自动
  保存视图数据功能。


I have a fragment that seems to be automatically restoring state over a screen rotation configuration change. I can verify in the logs that onCreatView is called in the fragment whenever the screen is rotated. Despite calling down to applyDefaults(), the draft entries that the user made are retained when the screen rotates and onCreateView() is called.

My understanding is that I would have to save state in onSaveInstanceState() and restore it in onCreateView(), but that doesn't seem to be the case. Can someone explain?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.d(TAG, "onCreateView()");
    View view = inflater.inflate(R.layout.fragment_email_entry, container, false);

    m_hostNameEditText = (EditText) view.findViewById(R.id.hostNameEditText);
    // set reference for other fields

    applyDefaultsForNewEmailAccount();
    return view;
}

private void applyDefaults() {
    m_hostNameEditText.setText("");
    // set other defaults
}

It may have to do with the fact that my Activity inherits from SingleFragmentActivity, so perhaps it sees that the fragment is already in the view. Still, we know that Fragment.onCreateView() is being called.

public abstract class SingleFragmentActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.single_fragment_activity);
        FragmentManager fragmentManager = getFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.single_frame_container);
        if (fragment == null) {
            fragment = createFragment();
            fragmentManager.beginTransaction()
                    .add(R.id.single_frame_container, fragment)
                    .commit();
        }
    }

    public abstract Fragment createFragment();
}

解决方案

Just like an activity, a fragment will automatically save the data of any fragment View component in the Bundle by the View component’s id. And just like in the activity, if you do implement the onSaveInstanceState( ) method make sure you add calls to the super.onSaveInstanceState( ) methods so as to retain this automatic save of View data feature.

source

这篇关于片段会自动保存和恢复EditTexts的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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