android - How to restart activity with data of previous or next position of a ListView -
i'm building book reader app , having issues on how move next or previous chapter.
the fragment in charge of showing list of chapter (basically listview). capture item onitemclick:
listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int i, long l) { intent intent = new intent(getactivity().getapplicationcontext(),readeractivity.class); intent.putextra("name",adapter.getitem(i).getname()); intent.putextra("url",adapter.getitem(i).geturl()); startactivity(intent); } }); it start readeractivity , display content of book. readeractivity has 2 buttons moving next/previous chapter. how can data (the name , url) listview?
at moment i'm thinking of 2 methods:
calling
finish()onreaderactivity, overrideondestroy()method call method next/previous data in fragment holds listview.passing whole arraylist of chapters
readeractivitycan retrieve item wants (probably not solution since there many chapters).
is there better way?
there way many ways best one.
some of them:
use
application.javashare in-memory data (data stores) place. example:public final application extends android.app.application { private list<chapter> chapters; public void setchapters(list<chapter> chapters) { this.chapters = chapters != null ? chapters : collections.<chapter>epmtylist(); } public list<chapter> chapters() { return chapters; } } // place has context (or context) list<chapter> chapters = ((application) getcontext().getapplicationcontext()).getchapters();make list static member public static getter , reference
readeractivitypublic final applicationactivity extends activity { private static list<chapter> chapters; .... public static list<chapter> getchapters() { return chapters; }store list in shared preferences activity can reference it
include list of chapters in intent extras , pass
readeractivity
i think readeractivity needs deal whole book rather single chapter. means approach (4) candidate.
Comments
Post a Comment