Skip to content

Commit

Permalink
ClassifyActivity/ClassifyFragment: Avoid IllegalStateException on res…
Browse files Browse the repository at this point in the history
…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 ee65f7b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
33 changes: 30 additions & 3 deletions app/src/main/java/com/murrayc/galaxyzoo/app/ClassifyActivity.java
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/murrayc/galaxyzoo/app/ClassifyFragment.java
Expand Up @@ -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();
Expand All @@ -281,6 +290,7 @@ public void onResume() {
update();
}
}
*/

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

0 comments on commit ee65f7b

Please sign in to comment.