6

I need to test an Activity onDestroy method followed by onCreate and onRestoreInstanceHandle. I know one way to do this - change screen orientation. But there is another situation when activity is destroyed - other application needs resources, and at some moment Android decides to destroy background activity. However, it still may be restored, with Bundle available. Is there some way to model such situation?

Alex F
  • 42,307
  • 41
  • 144
  • 212
  • I'm not sure, but when you start another Activity the onStop always get called. – Marcos Vasconcelos Oct 11 '12 at 13:34
  • @Marcos Vasconcelos - sorry, I mean onDestroy. The question is edited. – Alex F Oct 11 '12 at 13:36
  • remember that although onStop and onDestroy are usually called the system might decide to skip it if necessary by memory constrains. The best way to test it without rotating the device would be start opening other apps until your's is destroyed. – Budius Oct 11 '12 at 13:38
  • May be you should rename your question? Because it's really about testing activity lyfecycle and recreate. – Dmitry Ryadnenko Oct 11 '12 at 13:44
  • @Budius - this is what I am trying to do, unsuccessfully for now. Maybe I need to try harder... – Alex F Oct 11 '12 at 13:54
  • 1
    I was going to add that on ICS+ you can open the recent application and swipe the application out of the way. But the accepted answer looks even better. – Budius Oct 11 '12 at 14:21
  • @Budius - what is ICS+? I am still interesting in different options. – Alex F Oct 11 '12 at 14:27
  • Ice Cream Sandwich or later. Sorry for the confusion. – Budius Oct 11 '12 at 14:44

3 Answers3

7

I'm not sure what version of Android this became available on, but in at least Jelly Bean, there's a developer option that makes this really easy. Under Settings → Developer Options, scroll down to the Apps section and enable the Don't keep activities option.

Then, any time you exit an app (either with the Back button or the Home button), the OS will destroy that app's activity instead of just pausing it and putting it in the background. Don't forget to unset that setting when you're done testing, though.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
2

you can use this example and is written to log OnDestroy is 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");
    }
Jamshid
  • 340
  • 3
  • 12
2
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: Activity docs, ActivityInstrumentationTestCase2 docs, Instrumentation docs

MageWind
  • 785
  • 1
  • 6
  • 16