How to restart an Android app?

Hi,
I am trying to do an scenario which involves restarting the application under test and I cannot find a simple solution on how to do this without loosing the app current settings. Here’s the scenario:

 1. Start the app, do a login and get all docs related to a logged in user
 2. Disable WiFi
 3. Restart the app -> the state of the app should remain the same: i.e. user still logged in, no reset
 4. Do stuff in the app

Using Appium Java client, I tried calling driver.reset(), obviously it deletes all the settings. I also tried driver.closeApp() followed by driver.launchApp(), it actually does the same thing, it deletes the settings so the app is in a clean state. Any ideas?

If you were to do this manually, what would you do in step 3? That is, if you are restarting the app, would you just click the recent apps button and choose your application, or find the icon representing your app and click that? Or is there some other way to restart your app?

If you are just switching back to your application, you can do something similar to this ruby solution:

app_hash = {
    app_package: settings.app_package,
    app_activity: settings.app_activity
}
appium_driver.start_activity app_hash

settings.app_package and app_activity are the capabilities you passed to appium. It may be possible to not even specify those if you are just using the capabilities, but I haven’t researched that yet.

No, I would actually kill the app from task manager and restart it. Something that reset() method does, but without cleaning up its data…

How about using the adb command on point 3) and then try launchApp

adb shell am force-stop <PACKAGE>

I did not test it, just throwing ideas…

1 Like

Can I do this from code? Or is there any method implementing this in the java client?

I’m doing it in Ruby, but in java you can probably use Runtime class to execute OS commands

driver.closeApp() should not delete any app data. Are you sure “No Reset” button in Appium checked.

But closeApp does not kill it, only closed the app. I think he wants to force kill the app

As Telmo said, you can try his command in this way:
Runtime.getRuntime().exec("adb shell am force-stop ");

In ruby using following code:

start_driver
driver_quit

Will restart the driver :slight_smile:

Hi All,

i have set the below capabilities to restart the app without reset ,

capabilities.setCapability(“FullReset”, false);
capabilities.setCapability(“noReset”, true);

But i am getting below error logs, please help me to solve this issues

info: [debug] Waiting for pkg “com.mobile.activity” and activity “com.mobile.activity.SplashActivity” to be focused
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
info: [debug] Getting focused package and activity
info: [debug] executing cmd: “C:\AndriodSDK\sdk\platform-tools\adb.exe” -s UUID shell “dumpsys window windows”
error: com.mobile.activity/com.mobile.activity.SplashActivity never started. Current: com.mobile.activity/com.mobile.activity.LoginScreen
info: [debug] Stopping logcat capture
info: [debug] Logcat terminated with code null, signal SIGTERM
info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:“shutdown”}
info: [debug] [BOOTSTRAP] [debug] Got command of type SHUTDOWN
info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:0,“value”:“OK, shutting down”}
info: [debug] [BOOTSTRAP] [debug] Closed client connection
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=.
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 0
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
info: [debug] [UIAUTOMATOR STDOUT] Test results for WatcherResultPrinter=.
info: [debug] [UIAUTOMATOR STDOUT] Time: 22.591
info: [debug] [UIAUTOMATOR STDOUT] OK (1 test)
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: -1
info: [debug] Sent shutdown command, waiting for UiAutomator to stop…
info: [debug] UiAutomator shut down normally
info: [debug] Cleaning up android objects
info: [debug] Responding to client with error: {“status”:1,“value”:{“message”:"Unable to launch the app: Error: com.mobile…activity/com.mobile.activity.SplashActivity never started. Current: com.mobile.activity/com.mobile.activity.LoginScreen

It seems that your application shows the splash screen only first time of the launch. And you are not resetting data so second time Appium is not able to get the splash activity.

Yes… first time i was registering the application and second time without resetting its move to login screen page for enter pin for the same… but i am getting above error .

  1. First time, when you launch app (Provide app path+Launch activity) to your capability
  2. Second time, where app is already installed and you want to launch another activity (this time provide only launch activity – Do Not provide app path)

Above two task you need to handle programmatic way.

Thanks @Priyank_Shah , I followed same, but not working… its giving same issues “Shell dumpsys window windows”.

are you providing wait activity in your capability?

1 Like

I agree, you need to set appWaitActivity to ‘.LoginScreen’

Thanks @Priyank_Shah and @willosser . its worked for me :smile:

Hi, I am new to appium. Can you please share the piece of code which helped in relaunching the application ? That would be helpful!

@deepthi_bapat, have you figured out how to set capabilities when you start Appium? A description of capabilities can be found here: http://appium.io/slate/en/master/?ruby#appium-server-capabilities. I’ve specifically pointed to appWaitActivity