Appium : How to switch from one native app to another native app

In this Post, I will be going to explain you that how to switch from one android native app to another app at run time using Appium tool. Let say if there is a scenario where you want to perform some operations 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 the scenario.


Lets look at the below mentioned scenario :

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 apps


import io.appium.java_client.remote.MobileCapabilityType;
import java.util.concurrent.TimeUnit;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

public class SwitchAppTest {

AndroidDriver driver = null;
String calculatorAppPackageName="";
String calculatorAppActivityName="";
String settingsAppPackageName="";
String settingsAppActivityName="";

public void setUp() throws MalformedURLException
DesiredCapabilities capabilities =;
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "");
capabilities.setCapability(MobileCapabilityType.APP_PACKAGE, calculatorAppPackageName);
capabilities.setCapability(MobileCapabilityType.APP_ACTIVITY, calculatorAppActivityName);
driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);


public void testApp() throws InterruptedException, MalformedURLException
//Perform calculation in calculator

//launch settings App
driver.startActivity(settingsAppPackageName, settingsAppActivityName);

//Switch OFF WIFI

//Re launch calculator App
driver.startActivity(calculatorAppPackageName, calculatorAppActivityName);

//Validate results
String result = driver.findElement(By.className("android.widget.EditText")).getText();
System.out.println("Result : " + result);
Assert.assertEquals("Incorrect Result", "12", 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 *