Android应用程序重新启动时崩溃/强制关闭 [英] Android App Restarts upon Crash/force close

查看:333
本文介绍了Android应用程序重新启动时崩溃/强制关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序是越来越重启强制关闭后,通过我的整个应用程序,包括20项活动,我靠的主要活动的静态数据。因此,一旦应用程序是越来越坠毁我所有的静态数据迷路,当应用程序自动重启实际上它并没有在操作任何重要的数据。

我的问题是,一旦崩溃,我想这个事情发生

  1. 如果应用程序崩溃,我不希望应用程序重新启动,而我希望所有的堆栈/任务与此应用程序相关的被消灭的内存。用户可以从重新开始
  2. 重新启动
  3. 如果我不能重新启动prevent的应用程序,至少我想preserve的基​​本数据,以便当应用程序重新启动我可以指定他们回来。此外,当它重新启动我想我的应用程序,从主要活动开始。

我知道,当活动崩溃的android系统将带来堆旁活动的前景,这就是原因我的应用程序产生多余的结果。同时我也通过Android开发者,但我才知道唯一的事情就是建立在清单的属性安卓finishOnTaskLaunch =真正的。但不幸的是,这是毫无帮助我。我想AP preciate您解决这个问题,也让我知道了原因,并分析帮助。

解决方案
  1. 最好的解决办法是,而不是使用静态数据,使用共享preferences 数据库存储数据,如果任何未捕获的异常时,显示如应用程序崩溃和报告发送给管理员,然后重新启动导致崩溃的活动。这样,用户可以继续使用该应用程序。

  2. 做同样的,而不是重新启动造成的异常重新启动应用程序的活动,但

创建用于处理类 unCaughtException

 公共类MyExceptionHandler工具
        java.lang.Thread.UncaughtExceptionHandler {
    私人最终语境myContext;
    私人最终类别<> myActivityClass;

    公共MyExceptionHandler(上下文的背景下,类<> C){

        myContext =背景;
        myActivityClass = C;
    }

    公共无效uncaughtException(线程的线程,Throwable异常){

        StringWriter的堆栈跟踪=新的StringWriter();
        exception.printStackTrace(新的PrintWriter(堆栈跟踪));
        通信System.err.println(堆栈跟踪); //你可以使用LogCat中得
        意向意图=新的意图(myContext,myActivityClass);
        字符串s = stackTrace.toString();
        //你可以使用这个字符串知道是什么引起的异常,并在其中活动
        intent.putExtra(uncaughtException
                异常是:+ stackTrace.toString());
        intent.putExtra(堆栈跟踪,S);
        myContext.startActivity(意向);
        //对于重新启动活动
        Process.killProcess(Process.myPid());
        System.exit(0);
    }
}
 

和在每一个活动创建这个类的一个对象,并将其设置为 DefaultUncaughtExceptionHandler

  Thread.setDefaultUncaughtExceptionHandler(新MyExceptionHandler(这一点,
            YourCurrentActivity.class));
 

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

  1. If the app crashes, I don't want the app to restart rather I want all the stack/task related with this app to be wiped out of memory. A user can restart it from the beginning again
  2. If I can't prevent app from restart, at least I want to preserve essential data so that when the app restarts I can assign them back. Also when it restarts I want my app to start from the main activity.

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.

解决方案

  1. Best solution would be instead of using Static data, use Shared Preferences or store data in a Database and if any uncaught Exception occurs, show a message like Application 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.

  2. Do the same but instead of restarting the Activity which caused the Exception restart the application.

create a class used to handle 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);
    }
}

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆