How to close an app

In one of my Android tests I need to force close the app (on my real device I do it by performing a swipe-close on the app switcher screen). For that I need to close the app without finishing the driver session.
I have tried the following options:

  1. driver.terminateApp(com.csg.securechat);, and got this error: Original error: 'com.csg.securechat' is still running after 500ms timeout

driver.closeApp(); Activity activity = new Activity(com.csg.securechat', com.csg.csg4.modules.splash.CsgStartupActivity); androidDriver.startActivity(activity);

On this second try, it does close the app, it opens again, but the following appears on the appium log:

[debug] [ADB] Found package: 'com.csg.securechat' and fully qualified activity name : 'com.csg.csg4.modules.home.CsgHomeActivity' [debug] [ADB] Incorrect package and activity. Retrying.

This message repeats untill the following error appears:
Error: Cannot start the 'com.csg.securechat' application. Visit https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/android/activity-startup.md for troubleshooting. Original error: 'com.csg.csg4.modules.splash.CsgStartupActivity' or 'com.csg.securechat.com.csg.csg4.modules.splash.CsgStartupActivity' never started

Note: if I try to set com.csg.csg4.modules.home.CsgHomeActivity as the app activity, the app doesn’t even open.

Am I going to the right place, or there’s a better way?

driver.launchApp();

-> it just restarts app.

package io.appium.java_client;

...

public interface InteractsWithApps extends ExecutesMethod {

    /**
     * Launches the app, which was provided in the capabilities at session creation,
     * and (re)starts the session.
     */
    default void launchApp() {
        execute(LAUNCH_APP);
    }
...

PS terminateApp by “bundleId” should close your app. If NOT check that your app has functionality to restart after close.

driver.launchApp()
This actually restarts the session too, and I need to keep the session data.

Isn’t there a way to restart just the app keeping the session?

I was able to do it, it was actually quite simple:
First I did a driver.closeApp() to close the app.
Then a driver.activateApp(String appPackage) to relaunch it without clearing the data.

2 Likes