android - Keep fragment state/data in BottomNavigationView -
i've mainactivity 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 bottomnavigationview; 3 fragments. want keep data , state of each fragment, during transaction, instagram application. mean, if load database data in fragment a, move fragment b , come fragment a, want see state. how can perform this? **update**
these lines of code handle selection of fragment , transaction. these lines of code in mainactivity , newinstance static method. fragment1, fragment2 , fragment3 3 different fragments, different layout , backend. said before, want keep state of fragment during usage of app, instagram app.
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(); }
Comments
Post a Comment