Sunday, 15 July 2012

android - Keep selected NavigationItem trough orientation changes -



android - Keep selected NavigationItem trough orientation changes -

i implemented android 3+ devices navigation of views trough actionbar navigationmode (drop_down_list).

getactionbar().setnavigationmode(actionbar.navigation_mode_list); spinneradapter mspinneradapter = arrayadapter.createfromresource(this, r.array.action_list, android.r.layout.simple_spinner_dropdown_item); getactionbar().setlistnavigationcallbacks(mspinneradapter, new onnavigationlistener() { @override public boolean onnavigationitemselected(int index, long arg1) { if(index == 0) selecthomeview(); else selectmainview(); homecoming true; } });

this works intended, on orientation changes, onnavigationitemselected called 1 time again index = 0, returning activity first view.

how can maintain state? , don't allow onnavigationitem called index 0 oncreate?

edit:

following kirill answer, there's possible store current inedx, there's 3rd view ins't selectable trough navigationlist, , if don't phone call setnavigationitemselected after oncreate automatically fires index = 0, returning application first view.

this problem.

you can extend next function excuted whenever state of activity might lost,

@override protected void onsaveinstancestate(bundle savedinstancestate) { super.onsaveinstancestate(savedinstancestate); // save state of drop downwards menu savedinstancestate.putint("selectedindex",mdropmenu.getselectedindex()); } @override protected void onrestoreinstancestate(bundle savedinstancestate) super.onrestoreinstancestate(savedinstancestate); // restore state of drop downwards menu mdropmenu.setselectedindex(savedinstancestate.getint("selectedindex")); }

note mdropmenu should replace object, , should utilize appropriate method on it

android android-actionbar navigationitem

No comments:

Post a Comment