How to test the Activity onDestroy method

I need to check the Activity onDestroy method, followed by onCreate and onRestoreInstanceHandle . I know one way to do this is to change the screen orientation. But there is another situation where activity is destroyed - resources are needed for another application, and at some point Android decides to destroy the background actions. However, it can still be restored with a Bundle . Is there any way to simulate such a situation?

+4
source share
3 answers

I'm not sure which version of Android has become available, but at least Jelly Bean has a developer option that makes this very easy. In the Settings → Developer Options section , go to the Services section and enable the Do not perform actions option.

Then, when you exit the application (using the "Back" button or the "Home" button), the OS will destroy this activity of the application, and not just pause it and put it in the background. However, be sure to cancel this setting when you are done testing.

+3
source

you can use this example and write to the OnDestroy log called

  @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.finish(); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.i("test", "OnDestroy is called"); } 
+2
source
 public class MyActivityTests extends ActivityInstrumentationTestCase2<MyActivity> { public void testLifecycle() { Activity activity = this.getActivity(); //do stuff to the activity this.getInstrumentation().callActivityOnStop(activity); activity = this.getActivity(); // this should call onCreate() and onRestoreInstanceHandle() // write assertions } } 

See also: Actions , ActivityInstrumentationTestCase2 docs , Tools Documentation

+1
source

Source: https://habr.com/ru/post/1439167/


All Articles