单击后退按钮后,碎片无法正确显示

时间:2016-06-28 13:02:32

标签: android android-fragments

我设计了一个由一些片段组成的应用程序。主屏幕,MainScreen包含一个工具栏和一个framelayout,它显示一些选项卡及其相应的片段。

主界面的布局文件

df1 = df.set_index(['name', 'name1', 'name2'])
        .groupby('cat')['score']
        .nlargest(3)
        .reset_index(name='score')

对于FrameLayout tab_and_view,布局如下所示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<include layout="@layout/toolbar"
/>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/tab_and_view" />
</LinearLayout>

我使用Google的SlidingTabLayout示例来构建标签。 然后我设计了一个页面作为主页,称为Home_Page。 Home_Page只有一个ListView项目可以显示一些选项,它将显示在MainScreen的ViewPager中。

Home_Page的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.android.luftschlafer.engdictionary.Tab.SlidingTabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorAccent"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1" />
</LinearLayout>

在Home_Page.java中处理交换的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Corr_Pages.HomePage">

<!-- TODO: Update blank fragment layout -->
<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/home_function_list"/>
</RelativeLayout>

我的设计是当我点击home_function_list中的一个项目时,片段将交换到另一个片段。例如,从Home_Page到New_Content。 在New_Content中,有一个可以返回home_page的按钮。

在按钮的onClickListener中,我使用这些代码来执行操作。

FragmentTransaction ftran=getFragmentManager().beginTransaction();
ftran.replace(R.id.tab_and_view,new NewContent());
ftran.addToBackStack("HomePage");
ftran.commit();

请参阅the picture after pressing back button in New_Content,我发现在单击“设置”和“关于应用程序”之前,“主页”和“搜索”选项卡的内容都丢失了。所以我想知道如何在按下&#34; Back&#34;之后再次显示标签中的所有内容。 New_Content中的按钮。

谢谢!

1 个答案:

答案 0 :(得分:0)

我不知道是否有更有效的方法,但是当我遇到碎片+活动+后退按钮这个问题时,我通过在我的相关活动中实现onBackPressed()来解决它,并在那里检查我的我的后台堆栈的片段管理器。基于该堆栈(这是允许我确定从哪里按下的位置)我可以正确地重建我的UI元素。在我的情况下,这样做很重要,因为我有一个工具栏作为应用程序操作栏,我需要在整个应用程序中导航时更改此工具栏的外观。

所以我所做的就是:

@Override
    public void onBackPressed() {
        if (mFragManager.getBackStackEntryCount() > 1) {

            mFragManager.popBackStack();
            getSupportActionBar().setTitle(R.string.action_settings);
            getSupportActionBar().setSubtitle("");


        } else if (mFragManager.getBackStackEntryCount() > 0) {
            mFragManager.popBackStack();

            getSupportActionBar().setTitle(R.string.app_name);
            getSupportActionBar().setIcon(null);

            mDrawerToggle.setDrawerIndicatorEnabled(true);
            statusbar.setVisibility(View.VISIBLE);;

            setTheme(R.style.AppTheme);


        } else {
            super.onBackPressed();
        }
    }
相关问题