0

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.prof_mohamed.movieapp">

<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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mohamed Atef
  • 69
  • 13
  • post your manifest please – David Wasser Aug 16 '16 at 10:03
  • @DavidWasser i have edited my post to include my Manifest.xml file, please review the current state of my Manifest file. – Mohamed Atef Aug 16 '16 at 18:13
  • Have you enabled the developer option "do not keep activities"? Please add logging to `onDestroy()` in each of your activities and see if this method is being called. I see nothing strange in your code or in your manifest. Are you testing on a real device or emulator? – David Wasser Aug 16 '16 at 18:49
  • how is the variable `mAdapter` declared? How an where is it in initialized? – David Wasser Aug 16 '16 at 18:53
  • the mAdapter is initialized in th onPostExecute, u can review my post again, u will find my onPostExecute method with the mAdapter initialization – Mohamed Atef Aug 17 '16 at 17:05
  • i am testing on a real device not emulator – Mohamed Atef Aug 17 '16 at 17:15
  • 1
    Dear @DavidWasser in developer option "do not keep activities" was enabled, i have disabled it, and now every thing is running, saving state in Activities navigation, Home button navigation, and screen rotation.......... Thanks very much dear .. Your comment is the best Solution – Mohamed Atef Aug 17 '16 at 17:37

3 Answers3

0

If you want to save state for later use, you can write necessary data in either sqlite db or persistence storage. By this kind of design you have to save all data required to make activity in same state. There are other options also but saving data in permanent storage fit for your need and safe. (Assuming you want an activity always persist it state event it destroyed) OnSaveInstanceState and OnRestoreInstanceState automatically save views state (view with id defined in xml) not only for rotation but also for activity kill by system for memory requirement. So you have to save view state also. (for ex edittext with entered data or selected check box etc)

Ramit
  • 416
  • 3
  • 8
  • Excuse me dear, can u comment here with a complete resource like a video describing what you say. Thanks – Mohamed Atef Aug 16 '16 at 18:36
  • I said a lot things, for which part you need clarification please mention, so I can provide you link – Ramit Aug 16 '16 at 18:41
  • I use a a custom Adapter to inflate image poster on my recyclerview. i wanna have a link for saving state for this recyclerview for screen rotation, Activities and Home button navigation using sqlite for saving view state or whatever – Mohamed Atef Aug 16 '16 at 18:54
  • Can you please share adapter code? Want to know your list/array data model to provide efficient solution. – Ramit Aug 16 '16 at 19:00
  • (Assuming you already know and implemented this)For orientation change follow this http://stackoverflow.com/questions/3542333/how-to-prevent-custom-views-from-losing-state-across-screen-orientation-changes – Ramit Aug 16 '16 at 19:09
  • For saving data permanently try this https://developer.android.com/training/basics/data-storage/index.html – Ramit Aug 16 '16 at 19:10
  • please, review my post to see my mAdapter variable initialization in the onPostExecute method – Mohamed Atef Aug 17 '16 at 17:09
  • answer is in disabling "do not keep activities" in developer options... thanks very much – Mohamed Atef Aug 17 '16 at 17:39
0

First of all, you are not using the savedInstanceState.getSerializable("mylist") correctly, you should save and cast the object in a variable and use it:

List list = (List) savedInstanceState.getSerializable("mylist");

At this point, The bundle is restored only in the onCreate method (for example on rotation when the activity is destroyed then re-created)

To support saving the instance state on activity navigation or when the home button is pressed (which call onStop then onStart), put the same logic in the if condition inside the onRestoreInstanceState which is called after onStart.

realkarim
  • 61
  • 1
  • 6
  • Dear Karim, when i put both of the following logics in the onRestoreInsatnceState inside if condition 1. mAdapter = (ImagesAdapter) savedInstanceState.getSerializable("mylist"); 2. list = (ArrayList) savedInstanceState.getSerializable("mylist"); mAdapter = new ImagesAdapter(getBaseContext(), list); In reality, it doesn't make any sense, it doesn't influence saving state – Mohamed Atef Aug 16 '16 at 18:52
  • answer is in disabling "do not keep activities" in developer options... thanks very much – Mohamed Atef Aug 17 '16 at 17:39
-1

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 ...

Mohamed Atef
  • 69
  • 13
  • By disabling do not keep activity your app may run fine for now. But you need to take care of this case also. This option is provide to test app for cases where system kill activity and claim memory and recreate it. Please search for it and study about this. Debug your code call these mthod onRestoreInstanceState and onSaveInstnaceState after disabling do not keep activtiy option – Ramit Aug 18 '16 at 17:26