fragments and onConfigurationChanged



我想做一些我做的活动,但在一个片段。我所做的是使用activities:

旋转设备时,首先停止活动重新启动android:configChanges="keyboardHidden|orientation|screenSize"

in my activity add:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
 setContentView(R.layout.main);
}

所以得到活动不重新启动,但重新加载main.xml,使用layout-land

现在我有一个显示viewpager的活动,它包含三个片段。一切正常。旋转检测在片段

public class FRG_map_web extends Fragment  {
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.i("myLogs", "Rotation");
    }

问题是,片段不使用setContentView(R.layout.main);这是代码:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.frg.myFragment, null); 

我尝试使用:

LayoutInflater inflater = inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.frg.myFragment, null); 
...
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.frg.myFragment, null); 
...
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
view = inflater.inflate(R.layout.frg.myFragment, null); 
...
LayoutInflater li = LayoutInflater.from(context);
和不同的方法,但总是没有成功我不能正常充气 谁能告诉我该怎么做?

提前感谢,我很感激你的帮助

如果我理解正确,你不希望片段在每次旋转时重新加载。相反,您需要重新布局视图并使用已有的信息更新它们。

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Get a layout inflater (inflater from getActivity() or getSupportActivity() works as well)
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View newView = inflater.inflate(R.layout.frg.myFragment, null);
    // This just inflates the view but doesn't add it to any thing.
    // You need to add it to the root view of the fragment
    ViewGroup rootView = (ViewGroup) getView();
    // Remove all the existing views from the root view.
    // This is also a good place to recycle any resources you won't need anymore
    rootView.removeAllViews();
    rootView.addView(newView);
    // Viola, you have the new view setup
}

根据文档(getView()), getView()返回相同的视图,你从你的onCreateView()返回,但它没有。它实际上返回你在onCreateView()中返回的视图的父视图,这正是你需要的。getView()将返回一个NoSaveStateFrameLayout的实例,它专门用于Fragments的根视图。

你考虑过保留你的片段实例吗?看到片段# setRetainInstance。

允许你的活动被重新创建(不指定android:configChanges),但保留你的片段实例跨方向的变化。如果所有繁重的工作都发生在Fragment#onCreate中,这应该工作得很好。onCreate()将不会被再次调用,因为片段没有被重新创建。

您可以尝试分离附加片段:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    FragmentManager fragmentManager = getFragmentManager();
    if (fragmentManager != null) {
        fragmentManager.beginTransaction().detach(this).commitAllowingStateLoss();
    }
    super.onConfigurationChanged(newConfig);
    if (fragmentManager != null) {
        fragmentManager.beginTransaction().attach(this).commitAllowingStateLoss();
    }
}

我可以通过在onConfigurationChanged:

中重新附加片段来做到这一点
   @Override
   public void onConfigurationChanged(Configuration newConfig)
    {
        getActivity().detachFragment(this);
        super.onConfigurationChanged(newConfig);
        ....
        getActivity().attachFragment(this);
    }

请记住,通过分离和附加您的片段,您将只与它的视图一起工作。但是片段状态被"保存"在片段管理器中。

也许你可以尝试使用

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.frg.myFragment, container, false);
}

。在任何情况下,碎片仍然要销毁和重新创建,为什么不让Android处理它自动重新启动活动?如果有数据需要保存,可以保存在onSavedInstanceState()中。Android不建议设置android:configChanges="keyboardHidden|orientation|screenSize"

最新更新