android - onCreate method called twice on orientation change -
why oncreate method inside resourcelistfragment called twice on orientation change? after first call app crashes because getactivity()
inside onpostexecute
returns null
, tried changing:
new httptask(this).execute("");
to
if(savedinstancestata == null) new httptask(this).execute("");
then on second call savedinstancestate null, mactivatedposition listview.invalid_position
. i'm using layout aliases activity_list replaced activity_twopane.
mainactivity
public class mainactivity extends activity implements tablistener, onnavigationlistener { /** * whether or not activity in two-pane mode, i.e. running on tablet * device. */ private boolean mtwopane = false; resourcereservationapp app = (resourcereservationapp)getapplication(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_list); mtwopane = getresources().getboolean(r.bool.has_two_panes); if(mtwopane){ // set action bar show tabs. final actionbar actionbar = getactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); final string[] dropdownvalues = getresources().getstringarray(r.array.dropdown); (string item : dropdownvalues) actionbar.addtab(actionbar.newtab().settext(item).settablistener(this)); }else{ actionbar actionbar = getactionbar(); actionbar.setdisplayshowtitleenabled(false); actionbar.setnavigationmode(actionbar.navigation_mode_list); final string[] dropdownvalues = getresources().getstringarray(r.array.dropdown); arrayadapter<string> adapter = new arrayadapter<string>(actionbar.getthemedcontext(), android.r.layout.simple_spinner_item, android.r.id.text1, dropdownvalues); adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); actionbar.setlistnavigationcallbacks(adapter, this); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu items use in action bar menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.activity_item_list, menu); return super.oncreateoptionsmenu(menu); } @override public void ontabselected(tab tab, fragmenttransaction ft) { if(tab.gettext().equals("resources")){ resourcelistfragment fragment = new resourcelistfragment(); getfragmentmanager().begintransaction() .replace(r.id.fragment_list, fragment).commit(); } if(tab.gettext().equals("reservations")){ reservationlistfragment fragment = new reservationlistfragment(); getfragmentmanager().begintransaction() .replace(r.id.fragment_list, fragment).commit(); } } @override public void ontabunselected(tab tab, fragmenttransaction ft) { // todo auto-generated method stub } @override public void ontabreselected(tab tab, fragmenttransaction ft) { // todo auto-generated method stub } @override public boolean onnavigationitemselected(int itemposition, long itemid) { final string[] dropdownvalues = getresources().getstringarray(r.array.dropdown); if(dropdownvalues[itemposition].equals("resources")){ resourcelistfragment fragment = new resourcelistfragment(); getfragmentmanager().begintransaction() .replace(r.id.activity_list, fragment).commit(); } if(dropdownvalues[itemposition].equals("reservations")){ reservationlistfragment fragment = new reservationlistfragment(); getfragmentmanager().begintransaction() .replace(r.id.activity_list, fragment).commit(); } return true; } }
activity_list.xml
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_list" android:layout_width="match_parent" android:layout_height="match_parent" />
resourcelistfragment
public class resourcelistfragment extends listfragment { private boolean mtwopane =false; /** * serialization (saved instance state) bundle key representing * activated item position. used on tablets. */ private static final string state_activated_position = "activated_position"; /** * current activated item position. used on tablets. */ private int mactivatedposition = listview.invalid_position; public resourcelistfragment() { } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mtwopane = getresources().getboolean(r.bool.has_two_panes); new httptask(this).execute(""); } @override public void onviewcreated(view view, bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); if(mtwopane){ getlistview().setchoicemode(listview.choice_mode_single); if (savedinstancestate != null && savedinstancestate.containskey(state_activated_position)) { setactivatedposition(savedinstancestate .getint(state_activated_position)); } } } @override public void onlistitemclick(listview listview, view view, int position, long id) { super.onlistitemclick(listview, view, position, id); // notify active callbacks interface (the activity, if // fragment attached one) item has been selected. ((resourcereservationapp)getactivity().getapplication()).setselectedresource((resource)(listview.getadapter().getitem(position))); if (mtwopane) { // in two-pane mode, show detail view in activity // adding or replacing detail fragment using // fragment transaction. setactivatedposition(position); bundle arguments = new bundle(); arguments.putstring(resourcedetailfragment.arg_item_id, id+""); resourcedetailfragment fragment = new resourcedetailfragment(); fragment.setarguments(arguments); getactivity().getfragmentmanager().begintransaction() .replace(r.id.detail_container, fragment).commit(); } else { // in single-pane mode, start detail activity // selected item id. intent detailintent = new intent(getactivity(), resourcedetailactivity.class); detailintent.putextra(resourcedetailfragment.arg_item_id, id); startactivity(detailintent); } } @override public void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); if(mtwopane) { // serialize , persist activated item position. outstate.putint(state_activated_position, mactivatedposition); outstate.putparcelable("as",(parcelable) getactivity()); } } /** * turns on activate-on-click mode. when mode on, list items * given 'activated' state when touched. */ private void setactivatedposition(int position) { if(mtwopane) if (position == listview.invalid_position) { getlistview().setitemchecked(mactivatedposition, false); } else { getlistview().setitemchecked(position, true); } mactivatedposition = position; } private class httptask extends asynctask<string, void, list<resource>> { string message="received"; resourcelistfragment fragment; progressdialog dialog; public httptask(resourcelistfragment fragment){ this.fragment=fragment; } @override protected void onpreexecute() { dialog = progressdialog.show(getactivity(), "loading...", "please wait...", true); } @override protected list<resource> doinbackground(string... credentials) { list<resource>resources=null; try { resources =resourcedao.getresourcesfromservice("gataric","gataric"); if(resources== null){ message = "error occured!"; return resources; } } catch (httpexception e) { message = e.getmessage(); } return resources; } @override protected void onpostexecute(list<resource> result) { dialog.dismiss(); if(result!=null) fragment.setlistadapter(new arrayadapter<resource>(getactivity(), android.r.layout.simple_list_item_activated_1, android.r.id.text1, result)); fragment.setactivatedposition(fragment.mactivatedposition); //toast.maketext(getactivity().getbasecontext(), message, toast.length_long).show(); } } }
activity_twopane.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginleft="16dp" android:layout_marginright="16dp" android:baselinealigned="false" android:divider="?android:attr/dividerhorizontal" android:orientation="horizontal" android:showdividers="middle" tools:context="com.example.resourcereservation.resourcelistactivity" > <framelayout android:id="@+id/fragment_list" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <framelayout android:id="@+id/detail_container" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" />
when orientation changes activity destroyed , re-created before re-creation onsaveinstancestate()
called. save data in onsaveinstancestate()
method. can restore state during oncreate()
or onrestoreinstancestate()
. see this
Comments
Post a Comment