1

In my app I try to save ArrayList state with putSerializable method in onSaveInstanceState. And restore it in onCreate and onRestoreInstanceState. In my app if there is no saved instance of this object it will be create from internet data. But all the time I try to test this solution ArrayList start to downloading from network. Can anyone help me?

public class Activity extends SherlockActivity {

ListView listView;
SlidingMenu slidingMenu;
ArrayList<HashMap<String, String>> newsList = null;
String url = "/////here_my_url////";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setTitle(R.string.news_vatican);
    listView = (ListView) findViewById(R.id.newslist);

    slidingMenu = new SlidingMenu(this);
    slidingMenu.setMode(SlidingMenu.LEFT);
    slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    slidingMenu.setFadeDegree(0.35f);
    slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
    slidingMenu.setMenu(R.layout.slidingmenu);
    slidingMenu.setBehindOffset(150);

    if (savedInstanceState != null){
        newsList = (ArrayList<HashMap<String, String>>) savedInstanceState.getSerializable("list");
        Log.e("Activity", "RESTORING");
    } else {
        Log.e("Activity", "PARSING NEW");
        JSONParser parser = new JSONParser(this);
        parser.execute(url);
        try {
            newsList = parser.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
            Log.e("Activity", "Failed to get newslist");
        } catch (ExecutionException e) {
            e.printStackTrace();
            Log.e("Activity", "Failed to get newslist");

        }

    }


    listView.setAdapter(new NewsAdapter(getApplicationContext(), newsList));

}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putSerializable("list", newsList);
    super.onSaveInstanceState(savedInstanceState);
}

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

        newsList = (ArrayList<HashMap<String, String>>) savedInstanceState.getSerializable("list");

}
kostya17
  • 403
  • 5
  • 8
  • There is a difference between restoring state and persisting data. Is the ArrayList downloaded again even when you rotate the device from portrait to landscape(or vice versa)? – Vikram Aug 23 '13 at 21:17
  • I use only portrait orientation in my app android:configChanges="orientation" android:screenOrientation="portrait" – kostya17 Aug 23 '13 at 21:22
  • Well, in that case, `onSaveInstanceState(Bundle)` is kind of useless for you. What you need is a way to persist data across subsequent launches. Am I correct? – Vikram Aug 23 '13 at 21:25
  • Yes, when I open sliding menu, go to another activity and after that try launch to this activity my ArrayList start downloading from internet every time. But I want to restore this object. I hope you understand me. – kostya17 Aug 23 '13 at 21:33
  • How often do you need to update(download a fresh copy) the list? Possible solutions: use a database to store the entries, or write the ArrayList to internal storage. – Vikram Aug 23 '13 at 21:39
  • I think I will create "Refresh" button and user can refresh list themselves. And every time when user just open this Activity it will display old(saved) data. But now, the new arraylist download every time when user launch to this Activity – kostya17 Aug 23 '13 at 21:46
  • See my answer here: [Best method for storing contents of an ArrayList for android application?](http://stackoverflow.com/a/18369341/2558882). – Vikram Aug 23 '13 at 21:53

1 Answers1

0

If the list is not too large, one easy option would be to save it to Shared Preferences. However, you would need to serialize the ArrayList, as you can only store Strings.

You could try something like this in your onSaveInstanceState()...

  1. Get a SharedPreferences editor
    SharedPreferences.Editor editor = getSharedPreferences("shared_preferences", 0).edit();
  2. Add your value
    editor.putString("myList", serialize(myList));
  3. Commit the changes
    editor.commit()

Then later you can access this list anywhere, such as in your onCreate() method, even if your activity has been killed by doing

SharedPreferences preferences = getSharedPreferences("shared_preferences",0);
ArrayList<HashMap<String,String>> newsList = deserialize(preferences.getString("newsList", "");

Again, this would require the extra work of creating methods to serialize and deserialize your ArrayList, but it would certainly be an easy option if this is the only data you need to persist.

Frank Cangialosi
  • 433
  • 1
  • 4
  • 12