Skip to content

Commit

Permalink
ShadowFragmentActivity.OnRestoreInstanceState() stashes the bundle in…
Browse files Browse the repository at this point in the history
… the rehydrated Fragment so that it can be passed to the Fragment's OnActivityCreated() method, this is the same strategy Android uses to accomplish the same effect.

TestFragmentManager.addFragment() passes the Fragment's saved instance state to onActivityCreated()
  • Loading branch information
Amrit Thakur & Phil Goodwin committed Mar 27, 2012
1 parent 50309e5 commit ca8d471
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xtremelabs.robolectric.shadows;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
Expand All @@ -13,6 +14,7 @@ public class ShadowFragment {
protected View view;
protected FragmentActivity activity;
private String tag;
private Bundle savedInstanceState;

public void setView(View view) {
this.view = view;
Expand Down Expand Up @@ -55,4 +57,12 @@ public String getTag() {
public void setTag(String tag) {
this.tag = tag;
}

public void setSavedInstanceState(Bundle savedInstanceState) {
this.savedInstanceState = savedInstanceState;
}

public Bundle getSavedInstanceState() {
return savedInstanceState;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.List;
import java.util.Map;

import static com.xtremelabs.robolectric.Robolectric.shadowOf;

@Implements(FragmentActivity.class)
public class ShadowFragmentActivity extends ShadowActivity {
@RealObject
Expand Down Expand Up @@ -53,7 +55,11 @@ public void onRestoreInstanceState_forBogusActivityShadows(Bundle savedInstanceS
SerializedFragmentState fragmentState = (SerializedFragmentState) o;

try {
fragmentManager.beginTransaction().add(fragmentState.containerId, fragmentState.fragmentClass.newInstance(), fragmentState.tag).commit();
Fragment fragment = fragmentState.fragmentClass.newInstance();
shadowOf(fragment).setSavedInstanceState(savedInstanceState);
fragmentManager.beginTransaction()
.add(fragmentState.containerId, fragment, fragmentState.tag)
.commit();
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void addFragment(int containerViewId, String tag, Fragment fragment, bool
}

// These calls happen in the FragmentActivity's onStart in real Android
fragment.onActivityCreated(null);
fragment.onActivityCreated(shadowOf(fragment).getSavedInstanceState());
fragment.onStart();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import static com.xtremelabs.robolectric.Robolectric.shadowOf;
import static junit.framework.Assert.*;

@RunWith(WithTestDefaultsRunner.class)
Expand Down Expand Up @@ -102,6 +103,7 @@ public void onRestoreInstanceState_shouldRecreateFragments() throws Exception {
TestFragment restoredFrag = (TestFragment) fragmentManager.getFragments().get(containerId);
assertEquals(restoredFrag.getId(), dynamicFrag.getId());
assertEquals(restoredFrag.getTag(), dynamicFrag.getTag());
assertEquals(bundle, shadowOf(restoredFrag).getSavedInstanceState());
}

private static class TestFragmentActivity extends FragmentActivity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ public void getFragment_whenBundleSavedByShadowFragmentActivity_shouldGetFragmen
assertEquals(TestFragment.class, retrievedFragment.getClass());
}

@Test
public void addFragment_shouldPassSavedInstanceStateToOnCreateMethodOfFragment() throws Exception {
shadowOf(fragment).setSavedInstanceState(new Bundle());
manager.addFragment(CONTAINER_VIEW_ID, null, fragment, true);

assertTrue(fragment.onActivityCreated_savedInstanceState != null);
}

private static class TestFragmentActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.xtremelabs.robolectric.util.Transcript;

public class TestFragment extends Fragment {

public static final int FRAGMENT_VIEW_ID = 2323;
public boolean onAttachWasCalled;
public boolean onCreateWasCalled;
Expand All @@ -21,7 +20,8 @@ public class TestFragment extends Fragment {
public boolean onStartWasCalled;
public boolean onResumeWasCalled;
public Activity onAttachActivity;

public Bundle onActivityCreated_savedInstanceState;

Transcript transcript = new Transcript();

@Override
Expand Down Expand Up @@ -55,6 +55,7 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
onActivityCreatedWasCalled = true;
onActivityCreated_savedInstanceState = savedInstanceState;
transcript.add("onActivityCreated");
}

Expand Down

0 comments on commit ca8d471

Please sign in to comment.