Prof-Mohamed Atef Prof-Mohamed Atef - 3 months ago 35
Android Question

how onSaveInstanceState keep the latest state of the Activity even if the Activity destroyed or stoped

In this class, i am Overriding both of onSaveInstanceState and onRestoreInstanceState methods, it enabled me to keep the last state of the activity while rotating screen, however, if i navigated to another activity, or i went to Home of my device, it doesn't keep the latest state and start a new state.

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("mylist",mAdapter);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
savedInstanceState.getSerializable("mylist");

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ex) {
// Ignore
}
//setHasOptionsMenu(true);
recyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
if (savedInstanceState != null && savedInstanceState.getSerializable("mylist")!=null) {

savedInstanceState.getSerializable("mylist");
update_START();
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(mAdapter);
}else {
update_START();
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(mAdapter);
}
}


can any body tell me what should i add to inforce my Activity to keep its latest state even if the activity has been destroyed, should i add any block of code to any other activities ?

my onPostExecute method is as follows :

@Override
protected void onPostExecute(ArrayList<MovieEntity> result) {
mAdapter=null;
mAdapter = new ImagesAdapter(getBaseContext(), result);
recyclerView.setAdapter(mAdapter);
}


This is my Manifest.xml

enter code here



<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<receiver
android:name=".ConnectivityReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:label="Pop Movies">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DetailActivity" android:label="MovieDetail"></activity>
</application>




It supposed that when i navigate between activities or even between other applications, then return to my first running app, it should restore its latest saved state before starting navigation to other Activities or apps.

Please provide me with all possible solutions. your advice is appreciated. Thanks in advance.

Answer

in developer options "do not keep activities" option was enabled, you have to make it disabled, and now every thing is running, saving state in Activities navigation, Home button navigation, and screen rotation besides Overriding both of these methods

@Override

protected void onRestoreInstanceState(Bundle savedInstanceState) {

    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState != null && savedInstanceState.getSerializable("mylist")!=null) {

        savedInstanceState.getSerializable("mylist");
    }
}

@Override protected void onRestoreInstanceState(Bundle savedInstanceState) {

super.onRestoreInstanceState(savedInstanceState);
savedInstanceState.getSerializable("mylist");

}

@Override protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    savedInstanceState.getSerializable("mylist");

}

will make your app save its state completely ...