android:处理应用程序崩溃并启动一个特定的Activity

时间:2015-01-07 22:39:35

标签: android android-intent android-activity

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

这对我来说是一个问题,因为我有一些输入的用户信息在崩溃时丢失。

有没有办法强制应用程序从崩溃重启后从启动器屏幕启动?

感谢。

5 个答案:

答案 0 :(得分:13)

建议的解决方案1 ​​ -

将manifest.xml文件中的此标记android:clearTaskOnLaunch="true"添加到应始终启动的主活动中。

可能原因导致无效

当应用程序崩溃时,它会抛出Exception,我们需要处理Exception,否则我们就无法获得预期的行为

提议的解决方案2

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

  1. 创建一个扩展Application
  2. 的类
  3. 处理uncaughtException子类中的Application
  4. 在您的启动器Activity中,拨打Application课程。
  5. 在捕获异常后,启动主Activity(根据您的要求)。
  6. 代码示例

    第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();
    
            .............
        }
    }
    

    第4步

    根据您的要求修改以下方法

    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
    }
    

答案 1 :(得分:10)

我建议使用

等库

https://github.com/Ereza/CustomActivityOnCrash

由于图书馆负责其他东西以及不同版本的android。

enter image description here

答案 2 :(得分:0)

也许没有办法做到这一点,但你可以标记它,以便你知道活动是通过用户操作启动还是刚刚在崩溃后启动。

即,当您启动父活动时,将一些内容传递给startActivity意图。如果那不是那么它就是在崩溃后开始的。

答案 3 :(得分:0)

我设法用这样的intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);开始我的主要活动:

private void handleUncaughtException (Thread thread, Throwable e)
  {
    Intent intent = new Intent (getApplicationContext(), MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
  }

答案 4 :(得分:0)

首先在App

中创建并设置AndroidManifest.xml课程
     android:name=".App"
     android:clearTaskOnLaunch="true"

然后将此代码放在App类

public class App extends Application {
@Override
public void onCreate() {
    super.onCreate();
     Thread.setDefaultUncaughtExceptionHandler(
            new Thread.UncaughtExceptionHandler() {
                @Override
                public void uncaughtException(Thread thread, Throwable e) {
                    Log.d("AppCrash", "Error just lunched ");
                }
            });
}}

Debug Log Screen Shoot