android - fragment transaction back stack not working? -
so main activity contains 2 fragments. in activity_main.xml layout file have 1 (list)fragment , frame layout act container second fragment.
each time item clicked on list fragments list view, details corresponding item clicked should displayed in second fragment contained in frame layout.
that fine, works. when used fragment transaction reference create backstack, backstack not work expected.
what expected:
1) user clicks on item in list view, details corresponding itemclicked displayed on second fragment (with in frame layout).
2) user clicks on item in list view, details corresponding item replace details corresponding previous click.
3) user clicks button , previous item clicked on list view displayed along details corresponding item in second fragment.
what occurs:
i repeat steps 1 , 2 above. on step 3, app exits, in onstop method called followed ondestroy method.
this main activity.
import android.app.fragmenttransaction; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; public class mainactivity extends appcompatactivity implements workoutlistfragment.workoutlistlistener { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public void itemclicked(long id) { workoutdetailfragment details = new workoutdetailfragment(); fragmenttransaction ft = getfragmentmanager().begintransaction(); details.setworkout(id); ft.replace(r.id.fragment_container, details); ft.addtobackstack(null); ft.settransition(fragmenttransaction.transit_fragment_fade); ft.commit(); } }
edit:
(logcat required user):
08-27 22:18:10.850 12699-12699/? i/art﹕ not late-enabling -xcheck:jni (already on) 08-27 22:18:11.088 12699-12716/ramansb.workout d/openglrenderer﹕ use egl_swap_behavior_preserved: true 08-27 22:18:11.092 12699-12699/ramansb.workout d/﹕ hostconnection::get() new host connection established 0xb3ee7f00, tid 12699 08-27 22:18:11.106 12699-12699/ramansb.workout d/atlas﹕ validating map... 08-27 22:18:11.169 12699-12716/ramansb.workout d/﹕ hostconnection::get() new host connection established 0xb3fef0a0, tid 12716 08-27 22:18:11.186 12699-12716/ramansb.workout i/openglrenderer﹕ initialized egl, version 1.4 08-27 22:18:11.192 12699-12716/ramansb.workout d/openglrenderer﹕ enabling debug mode 0 08-27 22:18:11.217 12699-12716/ramansb.workout w/egl_emulation﹕ eglsurfaceattrib not implemented 08-27 22:18:11.217 12699-12716/ramansb.workout w/openglrenderer﹕ failed set egl_swap_behavior on surface 0xb3fe5080, error=egl_success
edit:
workout detail fragment:
import android.os.bundle; import android.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; public class workoutdetailfragment extends fragment { //all fragments require empty constructor ( no args ) public workoutdetailfragment(){} private long workoutid; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { if(savedinstancestate != null){ workoutid = savedinstancestate.getlong("workoutid"); } // inflate layout fragment return inflater.inflate(r.layout.fragment_workout_detail, container, false); } public void onstart() { super.onstart(); view view = getview(); //gets fragment's root view, gets activity class name if (view != null) { textview title = (textview) view.findviewbyid(r.id.texttitle); workout workout = workout.workouts[(int) workoutid]; title.settext(workout.getname()); textview description = (textview) view.findviewbyid(r.id.textdescription); description.settext(workout.getdescription()); } } public void setworkout(long id){ this.workoutid = id; } @override public void onsaveinstancestate(bundle savedinstancestate) { savedinstancestate.putlong("workoutid", workoutid); } }
workoutlist fragment:
import android.app.activity; import android.app.listfragment; import android.os.bundle; import android.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.listview; import android.widget.textview; /** * simple {@link fragment} subclass. */ //extends listfragment fragment list fragment public class workoutlistfragment extends listfragment{ interface workoutlistlistener{ void itemclicked(long id); } private workoutlistlistener listener; @override public void onattach(activity activity) { super.onattach(activity); //the activity has implemented workoutlistlistener interface therefore can casted type of workoutlistlistner this.listener = (workoutlistlistener) activity; } @override public void onlistitemclick(listview l, view v, int position, long id) { if(listener != null){ listener.itemclicked(id); } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { //creating array of workout names string[] names = new string[workout.workouts.length]; for(int i=0; i<names.length; i++){ names[i] = workout.workouts[i].getname(); } //creating arrayadapter, such listview(subclass of adapter view) can use data java array. arrayadapter<string> listadapter = new arrayadapter<string>(inflater.getcontext(), android.r.layout.simple_list_item_1, names); setlistadapter(listadapter); return super.oncreateview(inflater, container, savedinstancestate); //calling super class returns default layout listfragments } }
workout class containing data:
public class workout {
private string name; private string description; public static final workout[] workouts = { new workout("the limb loosener", "5 handstand push-ups\n10 1-legged squats\n15 pull-ups"), new workout("core agony", "100 pull-ups\n100 push-ups\n100 sit-ups\n100 squats"), new workout("the wimp special", "5 pull-ups\n10 push-ups\n15 squats"), new workout("strength , length", "500 meter run\n21 x 1.5 pood kettleball swing \n21 x pull-ups") }; //each workout has name , description private workout(string name, string description){ this.name = name; this.description = description; } public string getdescription(){ return description; } public string getname(){ return name; } public string tostring(){ return this.name; } }
@override public void onbackpressed() { fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.popbackstack(); if (fragmentmanager.getbackstackentrycount() == 0) super.onbackpressed(); }
also check out post use host gpu. couldn't error testing on nexus 7 4.4.4 physical device , nexus 5 api 21 on emulator
Comments
Post a Comment