java - Can't save instance state in android -


in app try save arraylist state putserializable method in onsaveinstancestate. , restore in oncreate , onrestoreinstancestate. in app if there no saved instance of object create internet data. time try test solution arraylist start downloading network. can 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 newslist");         } catch (executionexception e) {             e.printstacktrace();             log.e("activity", "failed 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");  } 

if list not large, 1 easy option save shared preferences. however, need serialize arraylist, can store strings.

you try in onsaveinstancestate()...

  1. get sharedpreferences editor
    sharedpreferences.editor editor = getsharedpreferences("shared_preferences", 0).edit();
  2. add value
    editor.putstring("mylist", serialize(mylist));
  3. commit changes
    editor.commit()

then later can access list anywhere, such in oncreate() method, if activity has been killed doing

sharedpreferences preferences = getsharedpreferences("shared_preferences",0); arraylist<hashmap<string,string>> newslist = deserialize(preferences.getstring("newslist", ""); 

again, require work of creating methods serialize , deserialize arraylist, easy option if data need persist.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -