Android 应用程序在崩溃/强制关闭时重新启动 [英] Android App Restarts upon Crash/force close
问题描述
我的 android 应用程序在强制关闭后重新启动,整个应用程序包含 20 个活动,我依赖于在主要活动上创建的静态数据.因此,一旦应用程序崩溃,我的所有静态数据都会丢失,并且当应用程序自动重新启动时,它实际上没有任何基本数据可供操作.
My android app is getting restarted after force close, through my entire application which consist of 20 activities, I am relying on static data created on a main activity. So once the app is getting crashed all my static data is getting lost and when the app auto restarts practically it does not have any essential data to operate upon.
我的问题是,在崩溃时我希望这件事发生
My question is, upon crash i want this things to happen
- 如果应用崩溃,我不希望应用重新启动,而是希望从内存中清除与此应用相关的所有堆栈/任务.用户可以从头开始重新启动它
- 如果我不能阻止应用重新启动,至少我想保留基本数据,以便在应用重新启动时可以将它们分配回来.此外,当它重新启动时,我希望我的应用从主要活动开始.
我知道当活动崩溃时,android 系统会将堆栈中的下一个活动带到前台,这就是我的应用程序产生冗余结果的原因.我也通过了 android 开发人员,但我只知道在 Manifest android:finishOnTaskLaunch="true"
中设置一个属性.但不幸的是,这对我没有帮助.非常感谢您帮助解决这个问题,并告诉我原因和分析.
I know when activity crashes android system will bring next activity in stack to foreground, and this is reason for my app producing redundant results. also i went through the android developers but only thing i got to know was setting up an attribute in Manifest android:finishOnTaskLaunch="true"
. But unfortunately this is of no help to me. I would appreciate your help on solving this issue, and also letting me know the cause and analysis.
推荐答案
最好的解决方案不是使用静态数据,而是使用
Shared Preferences
或将数据存储在Database
中,如果有任何uncaught Exception
发生时,显示类似Application has crashed and a report is sent to the admin
的消息,然后重新启动导致崩溃的 Activity.这样用户可以继续使用应用程序.
Best solution would be instead of using Static data, use
Shared Preferences
or store data in aDatabase
and if anyuncaught Exception
occurs, show a message likeApplication has crashed and a report is sent to the admin
and then restart the Activity that caused the Crash. This way user can continue using the application.
做同样的事情,但不要重新启动导致异常的 Activity 重新启动应用程序.
Do the same but instead of restarting the Activity which caused the Exception restart the application.
创建一个用于处理unCaughtException
public class MyExceptionHandler implements
java.lang.Thread.UncaughtExceptionHandler {
private final Context myContext;
private final Class<?> myActivityClass;
public MyExceptionHandler(Context context, Class<?> c) {
myContext = context;
myActivityClass = c;
}
public void uncaughtException(Thread thread, Throwable exception) {
StringWriter stackTrace = new StringWriter();
exception.printStackTrace(new PrintWriter(stackTrace));
System.err.println(stackTrace);// You can use LogCat too
Intent intent = new Intent(myContext, myActivityClass);
String s = stackTrace.toString();
//you can use this String to know what caused the exception and in which Activity
intent.putExtra("uncaughtException",
"Exception is: " + stackTrace.toString());
intent.putExtra("stacktrace", s);
myContext.startActivity(intent);
//for restarting the Activity
Process.killProcess(Process.myPid());
System.exit(0);
}
}
并在每个Activity中创建一个此类的Object并将其设置为DefaultUncaughtExceptionHandler
and in every Activity create an Object of this class and set it as the DefaultUncaughtExceptionHandler
Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler(this,
YourCurrentActivity.class));
这篇关于Android 应用程序在崩溃/强制关闭时重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!