Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

commitAllowingStateLoss on DialogFragment

I have an IllegalStateException on showing a DialogFragment :

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

i know why its happening but i want to using commitAllowingStateLoss on showing dialog by overriding DialogFragment show function :

public void show(FragmentManager manager, String tag) {     mDismissed = false;     mShownByMe = true;     FragmentTransaction ft = manager.beginTransaction();     ft.add(this, tag);     ft.commit(); //replace it by commitAllowingStateLoss } 

but i don't access to mDismissed and mShownByMe variables , how can i access those variables to modify them as it's parent did.

like image 753
Arash GM Avatar asked May 24 '15 13:05

Arash GM


People also ask

What does commitAllowingStateLoss () method do?

commitAllowingStateLoss():A transaction can only be committed with this method prior to its containing activity saving its state. If the commit is attempted after that point, an exception will be thrown. This is because the state after the commit can be lost if the activity needs to be restored from its state.

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

What is DialogFragment used for?

DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.

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

I think to prevent throwing IllegalStateException on DialogFragment might be better to use :

 YourDialogFragment dialogFragment = new YourDialogFragment();  fragmentManager.beginTransaction().add(dialogFragment, YourDialogFragment.TAG_FRAGMENT).commitAllowingStateLoss(); 

instead of using show() on DialogFragment.

like image 200
Arash GM Avatar answered Sep 24 '22 22:09

Arash GM