android - Fragment not attached to Activity when receiving callback from DialogFragment after rotating device -


i'm creating application allows user upload image gallery or take photo using device camera. when clicking appropriate button dialog appears , user can select 'take photo' or 'select image'. works fine, problem when rotate device while dialog on screen select option app crashes illegalstateexception saying fragment not attached activity. i've included dialog i'm using communicate target fragment:

creating dialogfragment:

public void onclick() {     // display dialog choice between pick/ take picture     listdialogfragment dialogfragment = listdialogfragment.newinstance(r.string.take_photo, r.array.add_picture_options_array);     dialogfragment.settargetfragment(this, 0);     dialogfragment.show(getfragmentmanager(), "moo"); } 

the dialogfragment class:

public class listdialogfragment extends dialogfragment {  private static final string title_resource_id = "title_resource_id"; private static final string array_resource_id = "array_resource_id";  private dialogitemselectedlistener listener;  public interface dialogitemselectedlistener {     void ontakephotoselected();      void onselectimageselected(); }  public listdialogfragment() {     // default empty constructor }  public static listdialogfragment newinstance(@stringres final int titleresourceid, @arrayres final int arrayresourceid) {     listdialogfragment dialogfragment = new listdialogfragment();     bundle bundle = new bundle();     bundle.putint(title_resource_id, titleresourceid);     bundle.putint(array_resource_id, arrayresourceid);     dialogfragment.setarguments(bundle);     return dialogfragment; }  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     try {         listener = (dialogitemselectedlistener) gettargetfragment();     } catch (classcastexception e) {         throw new classcastexception("calling fragment must implement dialogitemselectedlistener");     } }  @nonnull @override public dialog oncreatedialog(bundle savedinstancestate) {     final int title = getarguments().getint(title_resource_id);     final int listitems = getarguments().getint(array_resource_id);      return new alertdialog.builder(getactivity()).settitle(title).setitems(listitems, new dialoginterface.onclicklistener() {         @override         public void onclick(dialoginterface dialog, int which) {             switch (which) {                 case 0:                     listener.ontakephotoselected();                     break;                 case 1:                     listener.onselectimageselected();                     break;                 default:                     dismiss();             }         }     }).create(); } 

}

the fragment implements dialogitemselectedlistener , tries go following when ontakephotoselected() called:

private void takepicture() {     intent cameraintent = new intent(mediastore.action_image_capture);     // here, make sure activity handle camera intent exists     if (cameraintent.resolveactivity(context.getapplicationcontext().getpackagemanager()) != null) {         // photo go         imagefile = null;         try {             imagefile = createimagefile();         } catch (ioexception ex) {             // not create file         }          // @ point, can safely assume file created         if (imagefile != null) {             cameraintent.putextra(mediastore.extra_output, uri.fromfile(imagefile));             startactivityforresult(cameraintent, request_image_capture);         }     } } 

the activity:

public class testactivity extends actionbaractivity {  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.test_activity);     attachfragment(); }  private void attachfragment() {     getsupportfragmentmanager().begintransaction().replace(r.id.fragment_container, testfragment.newinstance()).commit(); } 

}

the app crashes when calling startactivityforresult();

java.lang.illegalstateexception: fragment mytestfragment{22b2a6a0} not attached activity 

what doing wrong here; behaviour expected? i've tried setretaininstance(true); not work.

the issue here each time orientation changes, you're creating new instance of fragment. , 1 of them attached activity. try this

    public class testactivity extends actionbaractivity {      testfragment testfragment;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.test_activity);         attachfragment(savedinstancestate);     }      private void attachfragment(bundle savedinstancestate) {         if (savedinstancestate == null){            testfragment = testfragment.newinstance();            getsupportfragmentmanager().begintransaction()            .replace(r.id.fragment_container, testfragment,     "testfragment").commit();     } else {       testfragment =  (testfragment)getsupportfragmentmanager().findfragmentbytag("testfragment");     } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -