简体   繁体   English

Activity重新创建时的FragmentTransaction hide()(配置更改/等)

[英]FragmentTransaction hide() upon Activity recreation(config change/etc)

FragmentTransaction's hide() does not persist when Activity gets recreated upon config change(rotation, etc). 当在配置更改(旋转等)时重新创建Activity时,FragmentTransaction的hide()不会持久。

It re-adds all fragments in the back-stack, so the fragments previously hidden becomes visible. 它将所有片段重新添加到后堆栈中,因此以前隐藏的片段变为可见。 Ex) Fragments A(hide), B(show), C(hide), D(hide) are in back stack. 例)片段A(隐藏),B(显示),C(隐藏),D(隐藏)位于后堆栈中。 When I rotate, for instance, it displays Fragment D on top after loading A, B, and C. 例如,当我旋转时,在加载A,B和C之后,它在顶部显示片段D。

    FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();

    ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.fade_in, R.anim.fade_out);

    // Hide all fragments other than the first
    for (int i = 1; i < fragments.length; i++) {
        if (fragments[i] != null) {
            ft.hide(fragments[i]);
        }
    }

    // Display only the first fragment
    if (fragments[0].isAdded()) {
        ft.show(fragments[0]);
    }
    else {
        ft.add(R.id.content_view, fragments[0], fragmentTag);
    }

    ft.commit();

Any workaround for this? 任何解决方法?

[Edit] Adding detail for the context of the problem. [编辑]添加问题背景的详细信息。

What I'm trying to accomplish is to have bottom bar(my custom view that simply tells me which tab has been tapped) that I can switch between fragments to the same state it was at. 我要完成的工作是使底部的条(我的自定义视图仅告诉我点击了哪个选项卡)可以在片段之间切换到与原来相同的状态。

An example is Instagram, Quora, and Google Plus apps. 一个示例是Instagram,Quora和Google Plus应用。 在此处输入图片说明

[Edit 2 in response to Scrotos] [编辑2以响应Scrotos]

This is in my Presenter class. 这是在我的Presenter类中。 loadFirstScreen() performs a fragment transaction above. loadFirstScreen()在上面执行片段事务。 Others are just initializations like setting contents and registering listeners. 其他只是设置内容和注册监听器之类的初始化。

public void onCreate(Bundle savedInstanceState) {
    mView.initActionbar();
    mView.initViews();
    mModel.getUserProfile().subscribe(new Subscriber<UserProfile>() {
        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onNext(UserProfile userProfile) {
            mView.initDrawer(userProfile);
        }
    });

    if (savedInstanceState == null) {
        mView.loadFirstScreen();
    }
}

In the applications you show they are probably removing/adding fragments based on the container Id. 在应用程序中,您显示它们可能正在基于容器ID删除/添加片段。 While also maintaining the fragments state using onSaveInstanceState and the savedInstanceState bundle in onCreate or onCreateView 同时还使用onCreate或onCreateView中的onSaveInstanceState和savedInstanceState捆绑包维护片段状态

The code in the question actually worked. 问题中的代码实际上有效。 One thing to note is to not set the actionbar title if hidden() is true in the fragments during recreation from config changes. 要注意的一件事是,如果在从配置更改中重新创建的过程中片段中的hidden()为true,则不要设置操作栏标题。

Thanks @Scrotos for chatting. 感谢@Scrotos聊天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM