【问题标题】:Android Instrumentaion: How do I go back to a previously launched Activity?Android Instrumentation:如何返回之前启动的 Activity?
【发布时间】:2010-02-19 22:03:45
【问题描述】:
我目前正在使用一个应用程序运行自动化测试,该应用程序使用多个 Activity 来显示每个屏幕。
在运行 Instrumentation 测试时是否可以返回到之前启动的 Activity?目前,当我使用 sendKeyDownUpSync(KeyEvent.KEYCODE_BACK); 时,这会强制我的测试退出,而不是返回到之前的 Activity。
非常感谢任何帮助。
晋
【问题讨论】:
标签:
android
android-activity
instrumentation
【解决方案1】:
您可以尝试在要关闭的 Activity 上调用 finish() 方法。
【解决方案2】:
酷。 finish() 运行良好 :) 当然,除非 Android 运行时不会终止先前的活动以释放资源。
【解决方案3】:
您也可以使用方法来执行此操作。
不推荐使用 Thia,但下面是对您有用的代码。
public void launchActivityCurrent() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
System.out.println("Inside Launch Activity current");
intent.setClassName("YourpackageName","Activity you want to launch");
For ex:
intent.setClassName("com.android.mms","com.android.mms.ui.ConversationList");
Context c = currentContext();
c.startActivity(intent);
}
我使用robotium,并且我不再调用instrumentation,所以我使用这种方式在应用程序活动中启动。
我希望这对你也有用。