OnCreateView多次调用/有动作条和片段工作 [英] OnCreateView called multiple times / Working with ActionBar and Fragments

查看:190
本文介绍了OnCreateView多次调用/有动作条和片段工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打开我的应用程序的一部分,从活动到片段,这样我可以用整齐的动作条的标签。

I switched part of my App from Activities to Fragments so that I can use the neat ActionBar tabs.

但是,在完成过渡我遇到了一个问题后:每当我切换到另一个选项卡,该片段被创建一遍。双方的onCreate和onCreateView被调用每次我得到一个标签的时间。

However, after completing the transition I ran into an issue: whenever I switch to another tab, that Fragment gets created all over again. Both onCreate and onCreateView get called every time I get to a tab.

予有4个标签,其中的每一个指打开这些片段之一:

I have 4 tabs, each of which is meant to open one of these fragments:

Fragment ShopFragment = new WebActivity();
Fragment SearchFragment = new SearchActivity(context);
Fragment StoreFragment = new StoreLocatorActivity(context, this);
Fragment BlogsFragment = new BlogsActivity(context, this);

下面是我的code的监听器:

Here's my code for the listener:

    class MyTabsListener implements ActionBar.TabListener {
        public Fragment fragment;

        public MyTabsListener(Fragment fragment) {
            this.fragment = fragment;
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            ft.hide(fragment);
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            ft.replace(R.id.fragment_container, fragment);
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {          

        }

    }

可能有人请点我朝着正确的方向?

Could someone please point me in the right direction?

推荐答案

当你调用<一个href="http://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#attach%28android.support.v4.app.Fragment%29">FragmentTransaction.replace(...), Android将有效地执行一个序列的<一个href="http://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#remove%28android.support.v4.app.Fragment%29">FragmentTransaction.remove(...) (适用于所有当前片段添加到该容器)和<一href="http://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#add%28int,%20android.support.v4.app.Fragment,%20java.lang.String%29">FragmentTransaction.add(...) (为您提供的片段)。除去一个片段的 FragmentManager 将引起片段被破坏,并且其状态将不再进行管理。最明显的,当你重新添加该片段的所有的意见都会被重置。注:因为你重复使用相同的片段情况下,碎片仍然会保持价值的实例变量

When you call FragmentTransaction.replace(...), Android will effectively perform a sequence of FragmentTransaction.remove(...) (for all Fragments currently added to that container) and FragmentTransaction.add(...) (for your supplied Fragment). Removing a Fragment from the FragmentManager will cause the Fragment to be destroyed and its state will no longer be managed. Most noticeably, when you re-add the Fragment all of the views will have been reset. Note: since you are reusing the same Fragment instance, the Fragment will still keep the value any instance variables.

一个解决这个问题的办法是使用<一href="http://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#detach%28android.support.v4.app.Fragment%29">FragmentTransaction.detach(Fragment)和<一href="http://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#attach%28android.support.v4.app.Fragment%29">FragmentTransaction.attach(Fragment)切换时。这将导致片段意见重新创建( onDestroyView()&安培; onCreateView()将被调用),但实例状态包将被保存和调用,所以视图状态可以维持在返还给你。这是采取<一的方法href="http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html">FragmentPagerAdapter当它试图片段之间切换。

One solution to this problem would be to use FragmentTransaction.detach(Fragment) and FragmentTransaction.attach(Fragment) when switching. This will cause the Fragment views to be recreated (onDestroyView() & onCreateView() will be called), but the instance state bundle will be saved and given back to you between calls and so the view state can be maintained. This is the approach taken by FragmentPagerAdapter when it tries to switch between Fragments.

另外,你可以让片段被摧毁,但独立维护自己保存的状态他们。这将使用较少的内存,在较慢的切换时间为代价。值得注意的方法是<一个href="http://developer.android.com/reference/android/app/FragmentManager.html#saveFragmentInstanceState%28android.app.Fragment%29">FragmentManager.saveFragmentInstanceState(Fragment)和<一href="http://developer.android.com/reference/android/app/Fragment.html#setInitialSavedState%28android.app.Fragment.SavedState%29">FragmentManager.setInitialSavedState(Fragment.SavedState),在同一起选择添加/删除。这是采取<一的方法href="http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html">FragmentStatePagerAdapter.

Alternatively, you could allow the Fragments to be destroyed, but maintain their saved state for them independently. This would use less memory, at the expense of a slower switching time. Methods of note would be FragmentManager.saveFragmentInstanceState(Fragment) and FragmentManager.setInitialSavedState(Fragment.SavedState), in conjuction with adding/removing. This is the approach taken by FragmentStatePagerAdapter.

您可以看看的<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.3_r1/android/support/v4/app/FragmentPagerAdapter.java#FragmentPagerAdapter">source为FragmentPagerAdapter 和<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.3_r1/android/support/v4/app/FragmentStatePagerAdapter.java#FragmentStatePagerAdapter">source对于FragmentStatePagerAdapter 的执行线索。

You can have a look at the source for FragmentPagerAdapter and the source for FragmentStatePagerAdapter for implementation hints.

这篇关于OnCreateView多次调用/有动作条和片段工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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