Appium : How to Switch from one Native App to Another Native App

In this Post, we will be going to discuss about that how to switch from one android mobile app to another app at run time using Appium tool. Let say, if you come up with scenario where where you want to perform some actions on one app and in between there is a need to access another app and then again you want to switch back to the original app in order to complete your test scenario. It’s not that difficult as Appium allows you to perform App switching during script execution.

Lets have a look at the scenario mentioned below

1. Launch Calculator App
2. Multiply 2 numbers
3. Launch Settings App
4. Switch Off the Wifi
5. Re-Launch Calculator app and then validate the multiplication result which you have performed in step 2.
6. Close both apps

NOTE : Just for reference, sharing the software versions used in below code is as follows :

Complete Code


import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.remote.MobileCapabilityType;

public class SwitchAppTest {
	public AppiumDriver<MobileElement> driver = null;

	String calculatorAppPackageName = "";
	String calculatorAppActivityName = "Calculator";
	String settingsAppPackageName = "";
	String settingsAppActivityName = "";

	public void setupstart() throws MalformedURLException {
		DesiredCapabilities capabilities =;
		capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
		capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
		capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "4100b79b459381f7");
		capabilities.setCapability("appPackage", calculatorAppPackageName);
		capabilities.setCapability("appActivity", calculatorAppActivityName);
		driver = new AndroidDriver<MobileElement>(new URL("http://localhost:4723/wd/hub"), capabilities);

	public void calcTest1() throws Exception {
		//Multiply 2 numbers in calculator app

		// launch settings App
		Activity activity = new Activity(settingsAppPackageName, settingsAppActivityName);
		((AndroidDriver<MobileElement>) driver).startActivity(activity);

		// Switch OFF WIFI

		// Re launch calculator App
		activity = new Activity(calculatorAppPackageName, calculatorAppActivityName);
		((AndroidDriver<MobileElement>) driver).startActivity(activity);
		String result = driver.findElement(By.className("android.widget.EditText")).getText();
		System.out.println("Result : " + result);


	public void tearDown() {

If you really like the information provided above, please don’t forget to leave the comment. You can also like us on Facebook.

Leave a Reply

Your email address will not be published. Required fields are marked *