Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show DialogFragment from onActivityResult

I have the following code in my onActivityResult for a fragment of mine:

onActivityResult(int requestCode, int resultCode, Intent data){    //other code    ProgressFragment progFragment = new ProgressFragment();      progFragment.show(getActivity().getSupportFragmentManager(), PROG_DIALOG_TAG);    // other code } 

However, I'm getting the following error:

Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState    

Anybody know what's going on, or how I can fix this? I should note I'm using the Android Support Package.

like image 920
Kurtis Nusbaum Avatar asked Apr 11 '12 21:04

Kurtis Nusbaum


People also ask

How do I get data from onActivityResult in fragment?

To get the result in your fragment make sure you call startActivityForResult(intent,111); instead of getActivity(). startActivityForResult(intent,111); inside your fragment. @StErMi Make sure you call startActivityForResult() and not getActivity().

How do I call onActivityResult in dialog?

There is a DialogFragment ( SettingsBackImageDialog ) opens by click on button in Fragment ( TrainerSettings ) in MainActivity. In SettingsBackImageDialog are some buttons, one of them is for take picture and set it for Imageview ( trainersettingsmainicon ) in Fragment.

How do I start a DialogFragment?

To create a DialogFragment , first create a class that extends DialogFragment , and override onCreateDialog() , as shown in the following example. Similar to how onCreateView() should create a root View in an ordinary fragment, onCreateDialog() should create a Dialog to display as part of the DialogFragment .

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.


1 Answers

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);     }   } } 
like image 168
Arcao Avatar answered Sep 18 '22 21:09

Arcao