Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Do not follow the <a href="http://developer.android.com/resources/tutorials/testing/activity_test.html#StateManagementTests" rel="nofollow">state management test example</a> : {dead link}</p> <pre><code>myActivity.finish(); myActivity = getActivity(); </code></pre> <p><code>ActivityInstrumentationTestCase2.getActivity()</code> starts the Activity the first time you call it, but then it simply returns <strong>that same Activity</strong> in each subsequent call in the test case. Thus, you are still looking at the Activity that you finished.</p> <p>After you finish the first Activity, you need to start a new one from the test. You can use <code>InstrumentationTestCase.launchActivity()</code>, for example. </p> <p>As another example, I've written a test that pushes a button in ActivityA that launches ActivityB for-result; the test then immediately kills ActivityA (via an orientation change, but finish() would work, too), and then the test gets a handle to the new ActivityA that the system creates when ActivityB is done and sends its result. The trick there was to have the test add an Instrumentation.ActivityMonitor and then have that monitor wait for the system to start the new ActivityA and give the test a handle to it.</p> <p>EDIT 2/23/2012 cdhabecker, Adding reproducible code:</p> <pre><code>public class VerboseActivity extends Activity { public final static String TAG = "Verbose"; @Override protected void onCreate(Bundle savedInstanceState) { Log.i(TAG, "onCreate() " + (Activity)this); super.onCreate(savedInstanceState); setContentView(R.layout.activity5); } @Override protected void onDestroy() { Log.i(TAG, "onDestroy()."); super.onDestroy(); } } </code></pre> <p>Test case: (the sleep() calls give the activity lots of time to respond)</p> <pre><code>public class VerboseTest extends ActivityInstrumentationTestCase2&lt;VerboseActivity&gt; { Activity myActivity = null; public VerboseTest() { super("com.scanillion.demo", VerboseActivity.class); } public void test_01() { String TAG = "test_01"; myActivity = getActivity(); Log.i(TAG, "A getActivity()=" + myActivity); myActivity.finish(); try { Thread.sleep(5000L); } catch (InterruptedException e) { } myActivity = getActivity(); Log.i(TAG, "B getActivity()=" + myActivity); try { Thread.sleep(5000L); } catch (InterruptedException e) { } } } </code></pre> <p>Log:</p> <pre><code>02-23 21:25:37.689: I/Verbose(17747): onCreate() com.scanillion.demo.VerboseActivity@43ba3360 02-23 21:25:38.159: I/ActivityManager(67): Displayed activity com.scanillion.demo/.VerboseActivity: 526 ms (total 526 ms) 02-23 21:25:38.180: I/test_01(17747): A getActivity()=com.scanillion.demo.VerboseActivity@43ba3360 02-23 21:25:38.540: I/Verbose(17747): onDestroy(). 02-23 21:25:43.236: I/test_01(17747): B getActivity()=com.scanillion.demo.VerboseActivity@43ba3360 02-23 21:25:48.439: I/TestRunner(17747): finished: test_01(com.scanillion.demo.test.VerboseTest) 02-23 21:25:48.439: I/TestRunner(17747): passed: test_01(com.scanillion.demo.test.VerboseTest) </code></pre> <p>Note that <code>finish()</code> caused <code>onDestroy()</code>, but the subsequent <code>getActivity()</code> was a no-op. Not only does getActivity() not instantiate a new Activity, it doesn't even recreate the original one.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload