I have the following code in my onActivityResult for a fragment of mine:
onActivityResult(int requestCode, int resultCode, Intent data){
//other code
P
just call super.onActivityResult(requestCode, resultCode, data);
before handling the fragment
It's an old question but I've solved by the simplest way, I think:
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
MsgUtils.toast(getString(R.string.msg_img_saved),
getActivity().getApplicationContext());
}
});
It happens because when #onActivityResult() is called, the parent activity has already call #onSaveInstanceState()
I would use a Runnable to "save" the action (show dialog) on #onActivityResult() to use it later when activity has been ready.
With this approach we make sure the action we want to will always work
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == YOUR_REQUEST_CODE) {
mRunnable = new Runnable() {
@Override
public void run() {
showDialog();
}
};
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onStart() {
super.onStart();
if (mRunnable != null) {
mRunnable.run();
mRunnable = null;
}
}
If you use Android support library, onResume method isn't the right place, where to play with fragments. You should do it in onResumeFragments method, see onResume method description: http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html#onResume%28%29
So the correct code from my point of view should be:
private boolean mShowDialog = false;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
// remember that dialog should be shown
mShowDialog = true;
}
@Override
protected void onResumeFragments() {
super.onResumeFragments();
// play with fragments here
if (mShowDialog) {
mShowDialog = false;
// Show only if is necessary, otherwise FragmentManager will take care
if (getSupportFragmentManager().findFragmentByTag(PROG_DIALOG_TAG) == null) {
new ProgressFragment().show(getSupportFragmentManager(), PROG_DIALOG_TAG);
}
}
}
I believe it is an Android bug. Basically Android calls onActivityResult at a wrong point in the activity/fragment lifecycle (before onStart()).
The bug is reported at https://issuetracker.google.com/issues/36929762
I solved it by basically storing the Intent as a parameter I later processed in onResume().
[EDIT] There are nowadays better solutions for this issue that were not available back in 2012. See the other answers.
I came up with a third solution, based partially from hmt's solution. Basically, create an ArrayList of DialogFragments, to be shown upon onResume();
ArrayList<DialogFragment> dialogList=new ArrayList<DialogFragment>();
//Some function, like onActivityResults
{
DialogFragment dialog=new DialogFragment();
dialogList.add(dialog);
}
protected void onResume()
{
super.onResume();
while (!dialogList.isEmpty())
dialogList.remove(0).show(getSupportFragmentManager(),"someDialog");
}