0

How is it possible to kill and restart a tested activity without calling onDestroy()?

The android-system can kill your app without calling onDestroy() (and even without calling onStop() in pre-Honeycomb), as in

In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy() callback, so it's important you use onStop() to release resources that might leak memory.

(see http://developer.android.com/training/basics/activity-lifecycle/stopping.html#Stop)

The following code gets the application to stop, but does not restart it again. It uses Robotium to check that the activity starts, but any method would be ok.

package com.testRestart;

import android.os.Bundle;
import android.test.ActivityInstrumentationTestCase2;
import com.robotium.solo.Solo;

public class RestartTest extends ActivityInstrumentationTestCase2<testactivity> {
    private Solo solo;

    public RestartTest() {
        super("com.testRestart", testactivity.class);
    }

    protected void setUp() throws Exception {
        solo = new Solo(getInstrumentation(), getActivity());
    }
    public void tearDown() throws Exception {
        solo.finishOpenedActivities();
    }

    public void testDestroyWithoutStop() {
        this.getInstrumentation().finish(23, new Bundle());// stops

        // getInstrumentation().runOnMainSync(new Runnable() {
        //  @Override public void run() {
        //        //    getActivity().finish();// does the same as recreate
        //      getActivity().recreate();
        //    }
        //});
        //setActivity(null);
        //getActivity();

        solo.waitForActivity(testactivity.class, 1000);
    }
}

The commented out code stops and restarts the activity, but calls all of onPause, onStop, and onDestroy.

(if you want evaluate this, go to Lifecycle Testing with Robotium: Killing and Restarting Activity for instructions on how to setup the activities)

The aborting behavior might be due to Robotium living inside the application.

It would be good to test the app's behavior concerning killing without onDestroy(), then restarting.

Any way to do this would be great.

How can it be done?

(if there is anything to improve in this question, please edit or leave a comment)

Community
  • 1
  • 1
serv-inc
  • 35,772
  • 9
  • 166
  • 188
  • 2
    I'm not familiar with Robotium, however, you could put a boolean in your Activity called shouldDestroy. Override onDestroy and if the flag is true, don't call super or run any of your code. In your test case, set the flag to true before you tear down. – blackcj Jul 08 '15 at 19:13
  • I had not thought of that. Nice approach. Do you see problems with modifying the source code in order to test (as this is not a real refactoring)? – serv-inc Jul 09 '15 at 10:10
  • @blackcj: (Robotium is just a tool to check whether the test worked. It is not relevant to the question.) – serv-inc Jul 09 '15 at 12:02
  • @blackcj: Thank you, but that approach gave a `android.app.SuperNotCalledException`, which could not be caught via try-catch. – serv-inc Jul 09 '15 at 17:31

1 Answers1

1

I'm not sure what you mean by Tested Activity. But if you want to restart your Activity then the following code should work fine.

Solution 1:

/**
 * Restarts the activity
 * @param context context
 */
public static void restartApp(final Context context){
    Intent restart = context
            .getPackageManager()
            .getLaunchIntentForPackage(context.getPackageName());
    restart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(restart);
}

Solution 2: Use recreate method of an Activity

Check the following link for more details

Source

Akshay Chordiya
  • 4,761
  • 3
  • 40
  • 52