首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >片段和onConfigurationChanged

片段和onConfigurationChanged
EN

Stack Overflow用户
提问于 2013-04-29 23:40:32
回答 5查看 24K关注 0票数 7

我正在尝试做一些我对活动所做的事情,但是在片段中。我所做的是使用活动:

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

在我的活动中添加:

代码语言:javascript
复制
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

 setContentView(R.layout.main);
}

因此,get活动不会重新启动,而是重新加载main.xml,以使用布局-土地

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

代码语言:javascript
复制
public class FRG_map_web extends Fragment  {

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        Log.i("myLogs", "Rotation");

    }

问题是片段没有使用setContentView(R.layout.main);下面是代码:

代码语言:javascript
复制
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.frg.myFragment, null); 

我试着使用:

代码语言:javascript
复制
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);

和不同的方式,但总是没有成功,我不能适当地膨胀。

有人能告诉我我该怎么做吗?

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

问候

EN

回答 5

Stack Overflow用户

发布于 2013-05-07 13:20:27

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

代码语言:javascript
复制
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的一个实例,该实例专门用于片段作为其根视图。

希望这能有所帮助。

票数 25
EN

Stack Overflow用户

发布于 2013-05-08 20:02:28

您是否考虑过保留片段实例?参见Fragment#setRetainInstance

允许重新创建您的活动(不要指定android:configChanges),但在方向更改时保留您的片段实例。如果所有繁重的任务都发生在Fragment#onCreate中,这应该可以很好地工作。将不会再次调用onCreate(),因为不会重新创建该片段。

票数 15
EN

Stack Overflow用户

发布于 2014-04-04 21:16:45

我可以通过在onConfigurationChanged中重新附加片段来实现:

代码语言:javascript
复制
   @Override
   public void onConfigurationChanged(Configuration newConfig)
    {
        getActivity().detachFragment(this);

        super.onConfigurationChanged(newConfig);

        ....

        getActivity().attachFragment(this);
    }

请记住,通过分离和附加您的片段,您将只使用它的视图。但是片段状态被“保存”在片段管理器中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16282739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档