Switching between apps during one session to retrieve information

I have a scenario in which
I have to launch an app,
Enter my last name and USER ID and click register - sends an activation code to gmail
My issue starte here - How do i switch to my gmail to get the activation code

Here is my code i have written which works till REGISTRATION.

package Pages;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.touch.TouchActions;
import org.openqa.selenium.remote.CapabilityType;
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.MobileDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.Activity;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;

public class BaseClassTablet {

public static void main(String[] args) throws MalformedURLException, InterruptedException {
	// TODO Auto-generated method stub

DesiredCapabilities cap = new DesiredCapabilities();

	cap.setCapability("deviceName", "Galaxy Tab S3");//setting > about phone
	cap.setCapability("udid", "624258263526a1e8");
	//udid is the device name when you press - cmd adb devices
	cap.setCapability("platformName", "Android");
	cap.setCapability("platformVersion", "8.0.0");
	cap.setCapability("appPackage", "com.verizon.launcher.sdnasit");
	cap.setCapability("appActivity", "com.verizon.launcher.webview.WebActivity");
//	cap.setCapability(MobileCapabilityType.APP, "Apk file location C:\\apkfiles\\AndroidUI.apk");
	
	//any application will have app package and app activity
	//download apk info from playstore

	URL url = new URL("http://127.0.0.1:4723/wd/hub");
	
	AndroidDriver <WebElement> driver = new AndroidDriver<WebElement>(url,cap);
	
	//navigation
	//clicking on element 
	
	
	
	//chick corea whihch is 3RD OPITON so use INDEX
	Thread.sleep(5000);
driver.findElement(By.id("iMSSO_Alert1_Continue_button")).click();
Thread.sleep(2000);
driver.findElement(By.id("lastName")).sendKeys("JOMON");
driver.findElement(By.id("USER")).sendKeys("JOMONLI");
driver.findElement(By.id("btnRegister")).click();

Why not try the gmail api?

https://developers.google.com/gmail/api/quickstart/java

It’s just a workaround i won’t recommend it. if you don’t find any other solution then get the Gmail launch activity using adb command.

adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'
``` command.

Then run activity launch command adb shell am start -an package name/.activityname by creating a process Runtime.getRuntime().exec("command")
Like this

Then with the driver you can look for the mail and come back to your test app by using driver.launchApp()