机器人:处理应用程序崩溃和启动特定活动 [英] android: Handle Application Crash and start a particular Activity

查看:167
本文介绍了机器人:处理应用程序崩溃和启动特定活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,如果在一个特定的活动应用程序崩溃,它在中间父活动之一重新启动。

I have an app and if the app crashes in a particular activity, it restarts at one of the intermediate parent activities.

这对我来说是一个问题,因为我有一个崩溃后失去了一些输入用户信息。

This is a problem for me since I have some inputted user info that is lost upon crash.

有没有什么办法来迫使应用程序从崩溃后重新启动从启动屏幕开始?

Is there any way to force the app to start from the launcher screen after restarting from a crash?

感谢。

推荐答案

添加此标记的android:clearTaskOnLaunch =真正的在manifest.xml文件到您的主要活动应该始终启动

Proposed Solution 1 -

Add this tag android:clearTaskOnLaunch="true" in the manifest.xml file to your main activity which should always launch.

可能的原因,为什么它不工作

当应用程序崩溃,它会抛出和异常,我们需要处理的异常,否则我们不会得到预期的行为

When the application crashes, it throws and exception and we need to handle the exception and otherwise we would not get the expected behavior

尝试处理任何未捕获的异常,并告诉系统做什么。要实现这一点,请尝试以下步骤。

Try to handle any uncaught exception and tell the system what to do. To implement this, try the below steps.


  1. 创建推广应用类的类

  2. 在您的应用程序子类处理uncaughtException。

  3. 在您的发射活动,打电话给你的应用程序类

  4. 捕获异常后,启动您的主要活动(根据您的要求)

步骤1和2

package com.casestudy.intentsandfilter;

import android.app.Application;
import android.content.Intent;

public class MyApplication extends Application
{
  @Override
  public void onCreate()
  {
    super.onCreate();

    Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
    {
      @Override
      public void uncaughtException (Thread thread, Throwable e)
      {
        handleUncaughtException (thread, e);
      }
    });

  }

  private void handleUncaughtException (Thread thread, Throwable e)
  {

    // The following shows what I'd like, though it won't work like this.
    Intent intent = new Intent (getApplicationContext(),DrawView.class);
    startActivity(intent);

   // Add some code logic if needed based on your requirement
  }


}

第3步

public class MainActivity extends Activity
{
  protected MyApplication app;

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Get the application instance
    app = (MyApplication)getApplication();

     .............
   }
}

第四步

修改下面的方法根据您的要求。

Modify the below method as per your requirement

private void handleUncaughtException (Thread thread, Throwable e)
      {

        // The following shows what I'd like, though it won't work like this.
        Intent intent = new Intent (getApplicationContext(),HomeActivity.class);
        startActivity(intent);

       // Add some code logic if needed based on your requirement
      }

这篇关于机器人:处理应用程序崩溃和启动特定活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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