如何恢复一个previously显示的片段? [英] How do I restore a previously displayed Fragment?

查看:306
本文介绍了如何恢复一个previously显示的片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的项目中兼容包v4和我有一个问题,保持片段后,它将从视图中移除左右。我有我的活动显示2片段......在左侧的菜单框,在右侧窗格中的内容。菜单帧具有3个不同的菜单(片段),其可以被显示。

I'm using the compatibility package v4 in my project and I'm having an issue with keeping a Fragment around after it's removed from view. I have my Activity displaying 2 fragments...a menu frame on the left, content pane on the right. The menu frame has 3 different menus (Fragments) that can be displayed.

这是我如何更换菜单片段:

This is how I'm replacing the menu Fragment:

public void showMenuFragment( Fragment fragment, String tag ) {

        showFragment( R.id.menu_frame, fragment, tag, false);
        setLastMenuPushed( tag );
    }

protected void showFragment( int resId, Fragment fragment, String tag, boolean addToBackStack ) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        if ( fragmentManager.findFragmentByTag( tag ) != null && fragment.isAdded() ) {
            transaction.remove( fragment );
        }

        transaction.replace( resId, fragment, tag ).setTransition( FragmentTransaction.TRANSIT_FRAGMENT_OPEN ).setBreadCrumbShortTitle( tag );

        if ( addToBackStack ) {
            transaction.addToBackStack( tag );
        }

        transaction.commit();
    }

菜单片段取值需要它可以显示之前加载数据,所以我表现出装载微调。一旦它的加载第一次,我想不会有永远载入了第二遍,除非活动已完成。

The menu Fragments require data to be loaded before it can be displayed, so I show a loading spinner. Once it's loaded the first time, I want to not have to ever load it a second time unless the Activity is finished.

当我称之为 showMenuFragment(...)我尝试通过在片段使用 FragmentManager.findFragmentByTag(字符串标签)但它总是,所以我必须做出一个新的片段每次我要显示不同的菜单。

When I call showMenuFragment(...) I try to pass in a Fragment using FragmentManager.findFragmentByTag(String tag) but it's always null, so I have to make a new Fragment every time I want to show a different menu.

我的问题是,如何把这些片段 s左右,他们被其他菜单后更换?我想避免饲养手册提到了片段是因为,当设备旋转,我的活动的实例类被破坏,我会失去这些引用。

My question is, how do I keep these Fragments around after they are replaced by other menus? I want to avoid keeping manual references to the Fragments because when the device is rotated, the instance of my Activity class is destroyed and I would lose those references.

编辑:摆在更加简单来说,我希望 FragmentManager 来跟踪previous 片段取值,而无需将它们添加到背面栈。我不想打后退按钮,看到显示的previous菜单。

To put in much simpler terms, I want the FragmentManager to keep track of previous Fragments without having to add them to the back stack. I don't want to hit the back button and see the previous menu shown.

推荐答案

只是理解了它。相反,当我添加新的删除片段,我只是隐藏起来,然后告诉他们,当我想再次显示它们。这里是我的修改方法:

Just figured it out. Instead of removing Fragments when I add new ones, I just hide them, then show them when I want to display them again. Here's my modified methods:

public void showMenuFragment( Fragment fragment, String tag, boolean addToBackStack ) {

        showFragment( R.id.menu_frame, fragment, tag, getLastMenuPushed(), addToBackStack );
        setLastMenuPushed( tag );
}

protected void showFragment( int resId, Fragment fragment, String tag, String lastTag, boolean addToBackStack ) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        if ( lastTag != null ) {
            Fragment lastFragment = fragmentManager.findFragmentByTag( lastTag );
            if ( lastFragment != null ) {
                transaction.hide( lastFragment );
            }
        }

        if ( fragment.isAdded() ) {
            transaction.show( fragment );
        }
        else {
            transaction.add( resId, fragment, tag ).setBreadCrumbShortTitle( tag );
        }

        if ( addToBackStack ) {
            transaction.addToBackStack( tag );
        }

        transaction.commit();
}

这篇关于如何恢复一个previously显示的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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