Robolectric onCreate with savedInstanceState, which contains ViewHierarchyState, FragmentManagerState
Using Robolectric, how can we test the Activity re-creation, which will simulate the case where Android re-creates the Activity in response to the back button (after the activity was destroyed by Android for RAM)?
On the device, the steps to reproduce this recreation of the Activity:
- enable "Do not perform actions" in developer settings.
- go to activity in your application.
- use the notification bar to activate a "Settings" action or other action on top of your activity, which will wipe out your Android activity as a simulated in-memory pressure.
- use the back button to reject the overlap operation (Settings)
As a result of the last step with the Back button, your activity receives an onCreate () with a nonzero saveInstanceState that contains information including a Bundle instance with key "android: viewHierarchyState" and, if fragments are used, a FragmentManagerState instance with key "android: support: fragments" ...
How can we simulate this in Robolectric? We're guessing we could do this with ActivityController.create (Bundle) if we can customize the bundle accordingly.
source to share
My couple got it! Use saveInstanceState () to ask Android to fill in the package you want:
ActivityController<SearchActivity> controller = ActivityController.of(SearchActivity.class); controller.create().start().resume().visible().get(); Bundle outState = new Bundle(); controller.pause().saveInstanceState(outState).stop(); controller = ActivityController.of(SearchActivity.class).create(outState).start().visible(); subject = controller.get(); // the create(Bundle) method was just called with the complete view hierarchy and fragment information
source to share