简体   繁体   English

在“强制关闭”之后返回到第一个活动

[英]Back into first activity after “Force close”

I have created very simple project that contains 3 activities. 我创建了一个非常简单的项目,其中包含3个活动。
activity 1 have a button can go to activity 2 活动1有一个按钮可以转到活动2
activity 2 have a button can go to activity 3 活动2有一个按钮可以转到活动3
activity 3 have a button crash 活动3发生按钮崩溃

on a crash button and I try set throw exception 在崩溃按钮上,我尝试设置throw exception

buttonCrash.setOnclickListener (new View.OnClickListener() {
    @Override
    public void onClick(View v){
        throw new NullPointerException();
    }
});

After crash I click re-open, and activity go back to activity 2. how to every crash the app will restart or clear activity stack? 崩溃后,我单击重新打开,活动返回到活动2。如何在每次崩溃时重新启动应用程序或清除活动堆栈?

I try to make handle exception class: 我尝试使句柄异常类:

public class MyExceptionHandler implements Thread.UncaughtExceptionHandler {
  private Activity activity;
  public MyExceptionHandler(Activity a) {
    activity = a;
  }
  @Override
  public void uncaughtException(Thread thread, Throwable ex) {
    Intent intent = new Intent(activity, MainActivity.class);
    intent.putExtra("crash", true);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
      | Intent.FLAG_ACTIVITY_CLEAR_TASK
      | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(MyApplication.getInstance().getBaseContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager mgr = (AlarmManager) MyApplication.getInstance().getBaseContext().getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, pendingIntent);
    activity.finish();
    System.exit(2);
  }
}

But it's suddenly restart the app, even though I need to show crash dialog first so I can click re-open to open again. 但是,即使我需要首先显示崩溃对话框,也可以突然重新启动该应用程序,因此我可以单击重新打开以再次打开。

在此处输入图片说明

try define your mainactivity home and default. 尝试定义您的主活动主页和默认主页。 add below code in intent-filter in activity in manifest: 在清单的活动中的意图过滤器中添加以下代码:

<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />

Add Activity clear task flag in every activity transition. 在每个活动转换中添加活动清除任务标志。

Intent intent = new Intent(this,yourclass.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

在第二个活动中添加finish()函数

Try using checking pass through on activity 1 尝试使用活动1上的检查传递

public static boolean DOING_PASS = false;

set DOING_PASS is true inside onStart on activity 1 activity 1 onStart内将DOING_PASS设置为true

@Override
void onStart(){
    DOING_PASS = true;
}

so You can checking on every activity except activity 1 inside onCreate 因此您可以检查onCreate内除活动1之外的所有活动

if(Activity1.DOING_PASS){
    // do normal
}
else{
    Intent i = new Intent(this, Activity1.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP || Intent.FLAG_ACTIVITY_CLEAR_TASK || Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM