A = 主要 Activity
B = 一些 Activity
正常情况
A > B > 主页按钮 > B应用程序被杀死的情况
A > B > Home Button > 闲置一段时间 > 应用程序被杀死 > 再次打开应用程序 > A
我怎样才能像案例 2 那样做?
P.S. 我尝试在 Manifest 中添加 android:launchMode="singleTask"
。它总是从 A 开始,但我只想在情况 2(应用程序被终止)中从 A 开始。
最佳答案
在您的应用程序类中使用这一行。此代码会自动将您的应用重定向到您想要的页面。如果它被杀死。
public class MYAppApplication extends Application {
Context context;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
context=this;
MySafetyMethod();
}
private void MySafetyMethod() {
// TODO Auto-generated method stub
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
System.out.println("inside the process of handling exceptions");
System.err.println("inside the process of handling exceptions");
ex.printStackTrace();
System.exit(2);
startActivity(new Intent(context, YourActivity.class));
}
});
}
}
希望对你有帮助。并且不要忘记在 list 中提及应用程序名称。
关于android - 应用程序被杀死时如何打开主要 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16604817/