Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to regain Access on a Activity after sending it to background

With Espresso I try to test sending an Activity to background, with the Home button and then getting it up in the foreground again to make some checks:

@EspressoTest
public void test() {
    onSomeView().check(matches(isDisplayed()));
    getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_HOME);

    Context context = getInstrumentation().getTargetContext();
    Intent intent = new Intent(context, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);

    onSomeView().check(matches(isDisplayed()));
}

I had to use intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); which was suggested by an exception, but apart of that I also tested, starting it as the Launcher Activity, or using FLAG_ACTIVITY_REORDER_TO_FRONT, but the view is not visible. Even though the test passes.

like image 726
joecks Avatar asked Apr 25 '14 12:04

joecks


People also ask

How will you restore activity if its destroyed when the app was in background?

Restore Your Activity State When your activity is recreated after it was previously destroyed, you can recover your saved state from the Bundle that the system passes your activity. Both the onCreate() and onRestoreInstanceState() callback methods receive the same Bundle that contains the instance state information.

What happens when app goes to background?

An app is running in the background when both the following conditions are satisfied: None of the app's activities are currently visible to the user. The app isn't running any foreground services that started while an activity from the app was visible to the user.

How do I open background activity?

Intent intent = new Intent(myActivity. this, myActivity. class); PendingIntent contentIntent = PendingIntent. getActivity(this, REQUEST_CODE, intent, 0); notification.


2 Answers

Please consider this response as it works 100% when you want to goBackgroundByClickingHomeThenGetBackToTheApp.

UiDevice device = UiDevice.getInstance(getInstrumentation());
        device.pressHome();
        device.pressRecentApps();
        device.findObject(new UiSelector().text(getTargetContext().getString(getTargetContext().getApplicationInfo().labelRes)))
                .click();
like image 82
AndroidLover Avatar answered Oct 14 '22 16:10

AndroidLover


Ok I figured it out. First it is necessary to use the Activity Context provided by getActivity, and then I could use intents to send the activity in background and in foreground, using the HOME category and the FLAG_ACTIVITY_REORDER_TO_FRONT:

private void goHome(Activity activity) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    activity.startActivity(intent);
}

private void bringToForeground(Activity activity) {
    Intent intent = new Intent(activity, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    activity.startActivity(intent);
}

@EspressoTest
public void test() {
    //do some ui stuff to ensure the activity is up and running
    goHome(getActivity());
    // eventually sleep, or implement an idling resource
    bringToForeground(getActivity());
    // do some ui tests
}
like image 35
joecks Avatar answered Oct 14 '22 15:10

joecks