I'm trying to test, if newly created Activity (after orientation change) is properly reinitialized. The code below shows that activity returned from getActivity() is the one constructed in setUp(), not the newly created one.
Test:
public class MyActivityTest extends ActivityInstrumentationTestCase2<MyActivity>{
private static final String TAG = "RAMPS";
private MyActivity mActivity;
public MyActivityTest() {
super("com.ramps", MyActivity.class);
}
protected void setUp() throws Exception {
super.setUp();
mActivity = getActivity();
Log.v(TAG, "setUp; activity=" + mActivity);
}
public void testOrienationChange(){
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getInstrumentation().waitForIdleSync();
MyActivity newActivity = getActivity(); //should be new, but it's not
Log.v(TAG, "testOrienationChange; activity=" + newActivity);
}
}
Activiy:
public class MyActivity extends Activity {
private static final String TAG = "RAMPS";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate; activity=" + this);
setContentView(new TextView(this));
}
//...rest of stuff like onSaveInstanceState() etc.
}
And logs:
06-11 14:16:52.431: V/RAMPS(367): onCreate; activity=MyActivity@44eb8690
06-11 14:16:52.891: V/RAMPS(367): setUp; activity=MyActivity@44eb8690
06-11 14:16:52.971: V/RAMPS(367): onCreate; activity=MyActivity@44ee5178
06-11 14:16:53.131: V/RAMPS(367): testOrienationChange; activity=MyActivity@44eb8690
As mentioned before, logs shows that new activity is indeed created (MyActivity@44ee5178), but getActivity() return the old activity, created in setUp() (MyActivity@44eb8690). Is it possible to access newly created one?