0

My application has this chain: the activity that creates fragment. In this fragment there is a button click which calls a DialogFragment. DialogFragment allows user to select camera or choose image from device memory.

Than DialogFragment calls intent which sends image to crop at new activity. And finaly path to croped image send back to DialogFragment via onActivityResult.

The resulting path should be passed to fragment and i exercise this using Interface through activity like in this lesson

This solution works fine until user no changes the orientation of the device.

When device rotated on DialogFragment stage or crop activity stage , first activity recreated and fragemnt recreated and instance of the DialogFragment is destroyed. So my callback not running and and nothing happens. Let me show you code:

SettingUnitActivity

FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        addNewUnitFragment = new AddNewUnitFragment();
        fragmentTransaction.replace(R.id.single_unit_fragment_container,addNewUnitFragment,"myff");
        fragmentTransaction.commit();

//My callback

@Override
public void call(String string) {
    AddNewUnitFragment addNewUnitFragment = (AddNewUnitFragment)
            getFragmentManager().findFragmentById(R.id.single_unit_fragment_container);
    if(addNewUnitFragment != null) {
        addNewUnitFragment.changeFragImage(string);
    } else {

    }
}

AddNewUnitFragment

public void onClick(View v) {
    switch (v.getTag().toString()) {
        case "add_image":
            DialogImageSelect imageSelDialog = DialogImageSelect.newInstance(0);
            imageSelDialog.show(getFragmentManager(),"imageSelDialog");
            break;

...

//Function which should be called from SettingUnitActivity 

public void changeFragImage (String string) {
//Do something with string
}

DialogImageSelect

public static interface dateCallBack {
    public abstract void call(String string);
}


static DialogImageSelect newInstance(int id) {
    DialogImageSelect di = new DialogImageSelect();
    Bundle args = new Bundle();
    args.putInt("id", id);
    di.setArguments(args);
    return di;
}


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
        callerActivity = (dateCallBack)activity;

}

//Here we take and send image path from croper activity

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

if(resultCode != Activity.RESULT_CANCELED) {
        if(requestCode == FROM_CROPPER) {
            if(data != null) {
                croppedFilePath = data.getStringExtra("filePath");
                System.out.println("CALL "+callerActivity);
                    this.callerActivity.call(croppedFilePath);
                dismiss();
            }
        }
    }
}

How can I send data to fragment retaining it when device rotated ?

UPD: The answer to my question is partially revealed in this topic but there is no explanation how I could pass data using interface callback from DialogFragment through Activity to Fragment if Activity recreated after rotate.

Community
  • 1
  • 1
MikaAll
  • 143
  • 1
  • 7

1 Answers1

0

Basically you need to save the instance state on fragment in onSaveInstanceState() method . then use this retained state inside the Base Activity's onCreate() method.

See:

Once for all, how to correctly save instance state of Fragments in back stack?

and

http://emuneee.com/blog/2013/01/07/saving-fragment-states/

You can google more on this and search stackoverflow. There are plenty of questions like yours with proper answers.

Community
  • 1
  • 1
Kushan
  • 5,855
  • 3
  • 31
  • 45