当闲置设备碎片状态丢失 [英] Fragment state lost when device left idle

查看:143
本文介绍了当闲置设备碎片状态丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

片段丢失状态,并显示是否闲置了20分钟的空UI。我使用 FragmentStatePagerAdapter ,我曾尝试致电 notifyDataSetChanged 在onStart()我FragmentActivity的。

Fragment losing state and shows an empty UI if left idle for 20 minutes. I'm using FragmentStatePagerAdapter and I have tried calling the notifyDataSetChanged in onStart() of my FragmentActivity.

请帮我如何保存我的片段的目标和状态,并重新使用它的应用程序重新加载。

Kindly help me how to save the object and state of my fragment and reuse it on reload of the app.

推荐答案

如果需要的Andr​​oid可以杀死你的应用程序,你需要使用<一个href=\"http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29\"相对=nofollow> 的onSaveInstanceState 保持在这个情况下,你的状态。 (记住:在 保存重要数据在onPause !)

Android can kill your app if needed, you need to use onSaveInstanceState to keep your state in this cases. (Remember: Save important data in onPause!)

<一个href=\"http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29\"相对=nofollow> 的onSaveInstanceState 存在于活动和片段,并以同样的方式使用类似的活动

onSaveInstanceState exists in Activity and Fragments and is used in the same way like an activity

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("integer", anInteger);
}

现在的<一个href=\"http://developer.android.com/reference/android/app/Fragment.html#onCreate%28android.os.Bundle%29\"相对=nofollow> 的onCreate ,<一个href=\"http://developer.android.com/reference/android/app/Fragment.html#onCreateView%28android.view.LayoutInflater,%20android.view.ViewGroup,%20android.os.Bundle%29\"相对=nofollow> onCreateView 或<一个href=\"http://developer.android.com/reference/android/app/Fragment.html#onActivityCreated%28android.os.Bundle%29\"相对=nofollow> onActivityCreated 你有这种说法的 捆绑 savedInstanceState 这corrisponds来保存套件。 (检查是否是空了。)

Now in onCreate, onCreateView or onActivityCreated you have this argument BundlesavedInstanceState which corrisponds to the bundle saved. (Check if it's null too.)

如果没有足够多的Andr​​oid或许杀了你的 FragmentManager ,所以你需要重写<一个href=\"http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29\"相对=nofollow> 的onSaveInstanceState 和<一个href=\"http://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState%28android.os.Bundle%29\"相对=nofollow> onRetoreInstanceState 在你的活动,并恢复片段。

If not enought maybe Android killed your FragmentManager too, so you need to override onSaveInstanceState and onRetoreInstanceState in your Activity and restore the fragment.

也许这个答案可以帮助你了解过去的事情我说:<一href=\"http://stackoverflow.com/questions/15608709/using-onsaveinstancestate-with-fragments-in-backstack\">Using用的onSaveInstanceState在backstack片段?

Maybe this answer could help you about the last thing i said: Using onSaveInstanceState with fragments in backstack?

这篇关于当闲置设备碎片状态丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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