commitAllowingStateLoss on DialogFragment
up vote
16
down vote
favorite
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.
android android-fragments android-dialogfragment dialogfragment
add a comment |
up vote
16
down vote
favorite
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.
android android-fragments android-dialogfragment dialogfragment
add a comment |
up vote
16
down vote
favorite
up vote
16
down vote
favorite
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.
android android-fragments android-dialogfragment 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.
android android-fragments android-dialogfragment dialogfragment
android android-fragments android-dialogfragment dialogfragment
asked May 24 '15 at 13:57
Arash GM
8,41444568
8,41444568
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
31
down vote
accepted
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
.
add a comment |
up vote
3
down vote
The solution about commitAllowingStateLoss works if your DialogFragment has no state to save, otherwise they will be lost like the function name told. But I think in most cases we have state to save, that is the major benefit of DialogFragment: Android recreate it and maintains its state automatically.
A better solution would be to check if the recreate process done, if not then return to caller, which is either an Activity or a FragmentActivity, it should call mark it and call the show function again later in its onPostResume() or onResumeFragments() callback, which we can make sure all fragments are recreated.
Here is an overridden show() from a subclass of DialogFragment:
public boolean show(FragmentManager fragmentManager) {
if (fragmentManager.isStateSaved()) return false;
show(fragmentManager, tagName);
return true;
}
add a comment |
up vote
0
down vote
origin dialog fragment
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
}
i don't know mDismissed
, mShownByMe
variables used for, so should be better if override show(FagmentManager, String)
method of DialogFragment and it works fine with me
override fun show(manager: FragmentManager?, tag: String?) {
if (manager?.isDestroyed == false && !manager.isStateSaved) {
super.show(manager, tag)
}
}
isStateSaved
available from appcompat >= 26.0.0 or androidx
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30424319%2fcommitallowingstateloss-on-dialogfragment%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
31
down vote
accepted
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
.
add a comment |
up vote
31
down vote
accepted
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
.
add a comment |
up vote
31
down vote
accepted
up vote
31
down vote
accepted
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
.
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
.
edited Dec 4 '17 at 12:39
Natali
2,65942747
2,65942747
answered May 25 '15 at 9:26
Arash GM
8,41444568
8,41444568
add a comment |
add a comment |
up vote
3
down vote
The solution about commitAllowingStateLoss works if your DialogFragment has no state to save, otherwise they will be lost like the function name told. But I think in most cases we have state to save, that is the major benefit of DialogFragment: Android recreate it and maintains its state automatically.
A better solution would be to check if the recreate process done, if not then return to caller, which is either an Activity or a FragmentActivity, it should call mark it and call the show function again later in its onPostResume() or onResumeFragments() callback, which we can make sure all fragments are recreated.
Here is an overridden show() from a subclass of DialogFragment:
public boolean show(FragmentManager fragmentManager) {
if (fragmentManager.isStateSaved()) return false;
show(fragmentManager, tagName);
return true;
}
add a comment |
up vote
3
down vote
The solution about commitAllowingStateLoss works if your DialogFragment has no state to save, otherwise they will be lost like the function name told. But I think in most cases we have state to save, that is the major benefit of DialogFragment: Android recreate it and maintains its state automatically.
A better solution would be to check if the recreate process done, if not then return to caller, which is either an Activity or a FragmentActivity, it should call mark it and call the show function again later in its onPostResume() or onResumeFragments() callback, which we can make sure all fragments are recreated.
Here is an overridden show() from a subclass of DialogFragment:
public boolean show(FragmentManager fragmentManager) {
if (fragmentManager.isStateSaved()) return false;
show(fragmentManager, tagName);
return true;
}
add a comment |
up vote
3
down vote
up vote
3
down vote
The solution about commitAllowingStateLoss works if your DialogFragment has no state to save, otherwise they will be lost like the function name told. But I think in most cases we have state to save, that is the major benefit of DialogFragment: Android recreate it and maintains its state automatically.
A better solution would be to check if the recreate process done, if not then return to caller, which is either an Activity or a FragmentActivity, it should call mark it and call the show function again later in its onPostResume() or onResumeFragments() callback, which we can make sure all fragments are recreated.
Here is an overridden show() from a subclass of DialogFragment:
public boolean show(FragmentManager fragmentManager) {
if (fragmentManager.isStateSaved()) return false;
show(fragmentManager, tagName);
return true;
}
The solution about commitAllowingStateLoss works if your DialogFragment has no state to save, otherwise they will be lost like the function name told. But I think in most cases we have state to save, that is the major benefit of DialogFragment: Android recreate it and maintains its state automatically.
A better solution would be to check if the recreate process done, if not then return to caller, which is either an Activity or a FragmentActivity, it should call mark it and call the show function again later in its onPostResume() or onResumeFragments() callback, which we can make sure all fragments are recreated.
Here is an overridden show() from a subclass of DialogFragment:
public boolean show(FragmentManager fragmentManager) {
if (fragmentManager.isStateSaved()) return false;
show(fragmentManager, tagName);
return true;
}
answered Aug 10 '17 at 19:21
cn123h
1,4291412
1,4291412
add a comment |
add a comment |
up vote
0
down vote
origin dialog fragment
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
}
i don't know mDismissed
, mShownByMe
variables used for, so should be better if override show(FagmentManager, String)
method of DialogFragment and it works fine with me
override fun show(manager: FragmentManager?, tag: String?) {
if (manager?.isDestroyed == false && !manager.isStateSaved) {
super.show(manager, tag)
}
}
isStateSaved
available from appcompat >= 26.0.0 or androidx
add a comment |
up vote
0
down vote
origin dialog fragment
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
}
i don't know mDismissed
, mShownByMe
variables used for, so should be better if override show(FagmentManager, String)
method of DialogFragment and it works fine with me
override fun show(manager: FragmentManager?, tag: String?) {
if (manager?.isDestroyed == false && !manager.isStateSaved) {
super.show(manager, tag)
}
}
isStateSaved
available from appcompat >= 26.0.0 or androidx
add a comment |
up vote
0
down vote
up vote
0
down vote
origin dialog fragment
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
}
i don't know mDismissed
, mShownByMe
variables used for, so should be better if override show(FagmentManager, String)
method of DialogFragment and it works fine with me
override fun show(manager: FragmentManager?, tag: String?) {
if (manager?.isDestroyed == false && !manager.isStateSaved) {
super.show(manager, tag)
}
}
isStateSaved
available from appcompat >= 26.0.0 or androidx
origin dialog fragment
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
}
i don't know mDismissed
, mShownByMe
variables used for, so should be better if override show(FagmentManager, String)
method of DialogFragment and it works fine with me
override fun show(manager: FragmentManager?, tag: String?) {
if (manager?.isDestroyed == false && !manager.isStateSaved) {
super.show(manager, tag)
}
}
isStateSaved
available from appcompat >= 26.0.0 or androidx
edited Nov 22 at 7:42
answered Nov 22 at 7:25
vuhung3990
4,13913037
4,13913037
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30424319%2fcommitallowingstateloss-on-dialogfragment%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown