There are three activities: Login
, Main
and Profile
Obviously from Login
you can go to Main
. There is a Button
that calls AppCompatDialogFragment
in Main
.
BalanceCalculationDialog dialog = BalanceCalculationDialog.newInstance(model);
dialog.setListener(this);
dialog.show(getSupportFragmentManager(), "TAG");
- If I user enters
Main
for the first time dialog shows. - If user goes to
Profile
and then backs dialog shows. If user goes out from
Main
(finish()
withIntent#FLAG_ACTIVITY_NEW_TASK
andIntent#FLAG_ACTIVITY_CLEAR_TASK
flags) and tries to seedialog
he sees an error.java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState....CheckStateLoss()...
As far as I know it caused because "Transaction cannot be commited after onSaveInstanceState()
".
I tried to override this method leaving empty body, not calling super
and didn't work.
Then I found another way to solve problem, which worked:
@Override
public void show(FragmentManager manager, String tag) {
try {
FragmentTransaction ft = manager.beginTransaction();
ft.add(this, tag);
ft.commitAllowingStateLoss();
} catch (IllegalStateException e) {
Log.e("ILLEGAL", "Exception", e);
}
}
Nevertheless I don't understand why it works this way -> Number of catching IllegalStateException
equals the number of recreating Main
. For example I log out 20 times - then I will have 20 catching of mentioned exception. So I don't find this solution as the right one.
In addition, I have other activities that can call another dialogs. These activities also can be finished with finish()
and when user opens one of them again he doesn't have the issue.
I would be very glad to have proper explanation of such behaviour and right way of solving it. Thank you very much.
EDIT
Reason of this issue was usage of RxJava
EventBus
. When I switched to Otto Eventbus
problem disappeared. RxJava
allows you to send and get messages without subscribe
and unsubscribe
that can cause unexpected behaviour.
Main