旋转时活动内容消失 [英] Activity content disappears when rotating

查看:61
本文介绍了旋转时活动内容消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有活动片段,并且轮播内容消失时.知道有什么问题吗?

Have fragment on activity, and when rotation content disappears. Any idea what is wrong?

public class MenuActivity extends AppCompatActivity {

    static MenuActivity menuActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        menuActivity = this;

        setContentView(R.layout.menu);

        App.setContext(this);

        if (savedInstanceState == null) {

            getFragmentManager().beginTransaction().add(R.id.frameLayout, new MessageListFragment()).commit();
        }
    }
}

推荐答案

在Fragment内部调用此方法:

inside Fragment call this method:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setRetainInstance(true);
}

还需要检查View是否为空,并防止重新创建它.

Also you need to check whether View is null or not, and prevent from recreating it.

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (view == null) {
        view = inflater.inflate(R.layout.fragment, container, false);
        // ...
    }
    return view;
}

这篇关于旋转时活动内容消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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