Permalink
Browse files

ClassifyActivity/ClassifyFragment: Avoid IllegalStateException on res…

…ume.

Avoid this exception:
  java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState.
caused by us using a FragmentTransaction during onResume().
Instead do it in the parent Activity's onResumeFragments(), where it
is apparently safer. This is very awkward Android API.
However, I cannot reproduce this after experiencing it once,
so it's hard to know if it's really fixed.
  • Loading branch information...
murraycu committed Nov 23, 2014
1 parent 838ae3a commit ee65f7b704a7a425cd1d0d6380c1433ab9482d02
@@ -282,6 +282,27 @@ protected void onPostExecute(final LoginUtils.LoginDetails loginDetails) {
task.execute();
}

/** This would ideally be in ClassifyFragment.onResume() or similar,
* but we need to do it here to avoid this exception sometimes:
* "java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState".
* as suggested here:
* http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html
*/
@Override
protected void onResumeFragments() {
super.onResumeFragments();

final ClassifyFragment fragmentClassify = getChildFragment();
if (fragmentClassify != null) {

if(TextUtils.equals(fragmentClassify.getItemId(), ItemsContentProvider.URI_PART_ITEM_ID_NEXT)) {
//We are probably resuming again after a previous failure to get new items
//from the network, so try again:
fragmentClassify.update();
}
}
}

private void onExistingLoginRetrieved(final LoginUtils.LoginDetails loginDetails) {
if (loginDetails != null && !TextUtils.isEmpty(loginDetails.authApiKey)) {
//Tell the user that we are logged in,
@@ -339,9 +360,15 @@ private void checkForLoginAsync() {
private void startNextClassification() {
//Start another classification:
setItemId(ItemsContentProvider.URI_PART_ITEM_ID_NEXT);
final ClassifyFragment fragmentClassify = (ClassifyFragment) getSupportFragmentManager().findFragmentById(R.id.container);
fragmentClassify.setItemId(ItemsContentProvider.URI_PART_ITEM_ID_NEXT);
fragmentClassify.update();
final ClassifyFragment fragmentClassify = getChildFragment();
if (fragmentClassify != null) {
fragmentClassify.setItemId(ItemsContentProvider.URI_PART_ITEM_ID_NEXT);
fragmentClassify.update();
}
}

private ClassifyFragment getChildFragment() {
return (ClassifyFragment) getSupportFragmentManager().findFragmentById(R.id.container);
}

@Override
@@ -271,6 +271,15 @@ public void update() {
}
}

/* We don't override this, to call update(),
* because that can sometimes lead to us using a Fragment Transaction at the wrong time,
* causing this exception:
* "java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState".
* Instead we do it in the parent activity's onResumeFragments() - see ClassifyActivty.onResumeFragments() .
* as suggested here:
* http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html
*/
/*
@Override
public void onResume() {
super.onResume();
@@ -281,6 +290,7 @@ public void onResume() {
update();
}
}
*/

private void updateFromCursor() {
if (mCursor == null) {

0 comments on commit ee65f7b

Please sign in to comment.