Reputation: 132
In my app, I have one activity and 9 fragments that are always replaced and kept on the back stack when the user goes from fragment 1 to fragment 2 the fragment 1's method onDestroyView is called and there i clean up all the fileds ( I have a big linkedlist with some custom object inside) and other objects that if i will keep them in memory, the OutOfMemory exception will be thrown.
Where should I store that list ? in a database ? in shared preferences ( here I saw that you need to make some hacks to make it possible). At first I thought to store them in the bundle provided by the onSaveInstanceState method but the method is NEVER called, because it's tight to the Activity which acts just as a "container"container for all the fragments.
And I think relating on OnSaveInstanceState is not a good practice inside the Fragment (for what I need). So in on Pause I do all the fields clean up and I should store them some where and when the fragment is re created get back all the variables an make the view as the user left it. ( Like keeping the position inside a recycler view and so on..)
I searched a lot regarding this and didn't find any concrete answear regarding this.
So to make it a bit clear, the question is the following:
What is the best practice in android, to save the state of Fragments(the position from a recycler view + the list of objects that is displaying + other fields) ? Should I use a DB that will store all the fields or in SharedPreferences ? Or is there another way that I didn't mentioned/find ?
My main concern in all of this is what are the best practices in android regarding this subject, and performance.
Upvotes: 2
Views: 242
Reputation: 132
After some more research, (in conclusion) I find that the best practice regarding this question is to use a database were you store all the attributes need for the recreation of the Fragment. Regarding the data base, I read about REALM library on which I did some research, and I find that it's a very easy to use library and a very complex.
See more details here: https://realm.io/docs/java/latest/
Upvotes: 1
Reputation: 4258
you may use TabHost with fragments it will automatically maintain the states This is my Main2Activity code
public class Main2Activity extends AppCompatActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("Tab 1", null),
Tab1Fragment.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("Tab 2", null),
Tab2Fragment.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab3").setIndicator("Tab 3", null),
Tab3Fragment.class, null);
}}
here is my activity_main2.xml
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Upvotes: 0
Reputation: 21
May be this can help. Android Developer ParceableYou can make the custom objects extend Parceable. Try lookin for Android Parceable example 2
Upvotes: 0