在BottomNavigationView中保留片段状态/数据

时间:2017-08-20 08:02:31

标签: android android-fragments android-viewpager bottomnavigationview

I've my MainActivity with the following layout:

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/lighter_gray"
        tools:context="com.rewardsme.merchant.activities.MainActivity">

        <FrameLayout
            android:layout_marginTop="?attr/actionBarSize"
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

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

        </FrameLayout>

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="?android:attr/windowBackground"
            app:menu="@menu/navigation" />

    </LinearLayout>

I've got 3 tabs in the BottomNavigationView; they are 3 fragments. I want to keep the data and state of each fragment, during the transaction, like the Instagram application. I mean, if I load from database some data in Fragment A, I move to Fragment B and I come back to fragment A, I want to see the previously state. How Can I perform this?

**UPDATE**

这些代码行处理片段及其事务的选择。这些代码行在MainActivity中,newInstance是一个静态方法。 Fragment1,Fragment2和Fragment3是3个不同的片段,具有不同的布局和后端。正如我之前所说,我希望在使用应用程序时保持片段的状态,例如Instagram应用程序。

        private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
                = new BottomNavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_1:
                        changeFragment(getSupportFragmentManager(), Fragment1.newInstance(), R.id.container);
                        return true;
                    case R.id.navigation_2:
                                            changeFragment(getSupportFragmentManager(), Fragment2.newInstance(), R.id.container);

                    case R.id.navigation_profile:
                                            changeFragment(getSupportFragmentManager(),Fragment3.newInstance(),  R.id.container);
                        return true;
                }
                return false;
            }

        };
public static void changeFragment(FragmentManager fragmentManager,
                                             Fragment fragment, int frameId){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(frameId, fragment);
        transaction.commit();
    }

0 个答案:

没有答案