保存导航抽屉片段状态 [英] Save fragment state with navigation drawer

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

问题描述

我要救一个片段的状态,而开关电源采用机器人导航抽屉片段。如果它已经previously装载的片段应该不会刷新。可能吗?

I want to save the state of a fragment while switching fragments using androids navigation drawer. The fragment shouldn't refresh if it has been previously loaded. Is it possible?

推荐答案

要保持一个片段的状态,你必须调用<一个href=\"https://developer.android.com/reference/android/app/Fragment.html#setRetainInstance%28boolean%29\"相对=nofollow> setRetainInstance(真) 内片段的的onCreate()。它所做的:

To keep an fragment's state you have to call setRetainInstance(true) inside the fragment's onCreate(). What it does:

片段实例控制是否可以通过活动保留
  重新创建(如从配置的变化)。

Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change).

这是保持状态,整个活动娱乐,但在这种情况下,你的活动将不会重新创建,您使用的是抽屉式手动切换片段。在这种情况下,而不是创建抽屉的点击监听器里一个新的片段,你必须寻找使用片段或者<一个href=\"http://developer.android.com/reference/android/app/FragmentManager.html#findFragmentById%28int%29\"相对=nofollow> findFragmentById(INT ID) 或<一个href=\"http://developer.android.com/reference/android/app/FragmentManager.html#findFragmentByTag%28java.lang.String%29\"相对=nofollow> findFragmentByTag(字符串标签) 。如果返回值是零,那么你可以安全地创建一个新的。

That keeps the state across the activity recreation, but in this case your activity won't be recreated, you are manually switching fragments using the drawer. In that case, instead of creating a new fragment inside the drawer's click listener, you'd have to look for the fragment using either findFragmentById(int id) or findFragmentByTag(String tag). If the value returned is null then you can safely create a new one.

这篇关于保存导航抽屉片段状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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