android-片段自动保存和恢复EditTexts的状态

android-片段自动保存和恢复EditTexts的状态,第1张

概述我有一个片段,似乎在屏幕旋转配置更改后会自动恢复状态.每当旋转屏幕时,我都可以在日志中验证片段中是否调用了onCreatView.尽管调用了applyDefaults(),但当屏幕旋转并调用onCreateView()时,用户所做的草稿条目将保留.我的理解是,我必须将状态保存在onSaveInstanceState()中,然后

我有一个片段,似乎在屏幕旋转配置更改后会自动恢复状态.每当旋转屏幕时,我都可以在日志中验证片段中是否调用了onCreatVIEw.尽管调用了applyDefaults(),但当屏幕旋转并调用onCreateVIEw()时,用户所做的草稿条目将保留.

我的理解是,我必须将状态保存在onSaveInstanceState()中,然后在onCreateVIEw()中进行还原,但这似乎并非如此.有人可以解释吗?

@OverrIDepublic 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}

这可能与我的Activity继承自SingleFragmentActivity的事实有关,因此也许它看到该片段已在视图中.不过,我们知道正在调用Fragment.onCreateVIEw().

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

总结

以上是内存溢出为你收集整理的android-片段自动保存和恢复EditTexts的状态全部内容,希望文章能够帮你解决android-片段自动保存和恢复EditTexts的状态所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1088469.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存