片段onCreateView多次调用 [英] fragment onCreateView called many times

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

问题描述

我要附加Activity的片段(许多片段到FrameLayout),每个片段在onCreateView()中都有自己的视图.

I'm attaching the fragment (many fragments to FrameLayout) of an Activity each fragment has its own view inflated in onCreateView().

现在

如果旋转屏幕风景/风景",则调用片段的onCreateView()而不是调用其附加的Activity的onCreate()方法.因此,视图被渲染了两次.

If I rotate the screen Landscape/Protrait the onCreateView() of the fragment is called instead of calling its attached Activity's onCreate() method. Because of this the view are rendered twice.

我希望每次配置更改时都调用Activity的onCreate().有可能吗?

I want the Activity's onCreate() to be called every time when there is a config changes. Is it possible?

活动:: MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    getSupportFragmentManager().beginTransaction()
        .add(R.id.layout_replace, new MyFragment()).commit();
}

片段: MyFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         
    return (ScrollView)inflater.inflate(R.layout.some_layout, container, false);
}

推荐答案

解决了这个问题!

FragmentTransaction的实际问题是我正在使用

Actual problem was with FragmentTransaction for which I'm adding fragments using .add() which causes the view to be populated twice.

代替使用:

.replace(R.id.yourId, fragment)

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

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