如何保持片段内存方向变化 [英] How to keep fragments in memory on orientation change

查看:104
本文介绍了如何保持片段内存方向变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用片段平板电脑优化的应用程序。我有一个薄导航片段下来左侧的按钮是什么片段将被装入到两个的ViewGroup容器,占用屏幕的其余部分的控制。

I'm creating a tablet optimised application using fragments. I have a thin navigation fragment down the left hand side with buttons to controls what fragments will be loaded into the two ViewGroup containers which take up the rest of the screen.

例如,如果在导航片段的一个按钮是pressed,第1容器将与项列表的片段(例如,用于设置,客户等)装载和第二容器将与片段来显示被装载项目的详细信息。

For example, if a button in the navigation fragment is pressed, the 1st container will be loaded with an item list fragment (eg for settings, customers etc) and the second container will be loaded with fragment to show the item details.

因为我想保留在内存中的片断(所以,从设置到客户浏览,然后再设置时,previous设置片段应该在那里,因此用户不会丢失他们在那里达) - 我已经创建片段的活动实例变量

Since I want to retain the fragments in memory (so, when navigating from Settings to Customers and then back to Settings, the previous Settings fragments should be there so the user doesn't lose where they were up to) - I have created fragments instance variables for the Activity

SettingsListFragment settingsListFragment;
SettingsDetailFragment settingsDetailFragment;
CustomerListFragment customerListFragment;
CustomerDetailFragment customerDetailFragment;

我然后创建这些片段时neccessary并根据需要与碎片更换容器。例如:

I then create these fragments when neccessary and replace the containers with the fragments as needed. eg.

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if(settingsListFragment == null){
   settingsListFragment = new SettingsListFragment();
}           

fragmentTransaction.replace(R.id.fragment_container_left, settingsListFragment);
fragmentTransaction.commit();

我的问题是,我怎么能留住这些实例翻过的方向改变?

My question is, how can I retain these instances accross an orientation change?

我假设我不应该使用按活动onRetainNonConfigurationInstance()方法
http://developer.android.com/resources/articles/faster-screen-orientation-change.html因为要非常小心你穿过onRetainNonConfigurationChange()的对象......这意味着你永远不应该通过视图,可绘制,适配器等

I'm assuming that I shouldn't use the activities onRetainNonConfigurationInstance() method according to http://developer.android.com/resources/articles/faster-screen-orientation-change.html because "Be very careful with the object you pass through onRetainNonConfigurationChange() ...This means you should never pass a View, a Drawable, an Adapter, etc"

我也尝试从片段经理拿到片段,
片段CLF = fragmentManager.findFragmentByTag(客户名单);
但回来为空。我觉得片段被替换之后被释放。

I've also attempted to get the Fragment from the fragment manager, Fragment clf = fragmentManager.findFragmentByTag("CLIENT LIST"); but it comes back as null. I think it gets released after the fragment is replaced.

感谢您的帮助/咨询。

推荐答案

您不能(或实在不应该)保持那样的片段。我猜这是说明你要保留。

You can't (or really shouldn't) retain fragments in that way. I'm guessing it's state you want to keep.

如果您存储在<一的状态href=\"http://developer.android.com/reference/android/app/Fragment.html#onSaveInstanceState%28android.os.Bundle%29\"相对=nofollow> 的onSaveInstanceState() 您可以稍后retrive它<一个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#onActivityCreated%28android.os.Bundle%29\"相对=nofollow> onActivityCreate() 。所以,以后你回去(你必须在 FragmentTransaction 添加到后退堆栈),你可以从捆绑到其中保存恢复状态!

If you store your state in onSaveInstanceState() you can later retrive it in onCreate() or onActivityCreate(). So after you go back (you'll have to add the FragmentTransaction to the back stack) you can restore the state from the Bundle to which you saved it!

FragmentManager 会照顾处理配置的变化,并在其目前的意见恢复您的片段。

FragmentManager will take care of handling configuration changes and restoring your fragments in their current views.

这篇关于如何保持片段内存方向变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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