serv-inc
serv-inc

Reputation: 38177

Lifecycle Testing: Restarting without onDestroy()

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)

Upvotes: 0

Views: 919

Answers (1)

Akshay Chordiya
Akshay Chordiya

Reputation: 4841

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

Upvotes: 1

Related Questions