Wis*_*erd 13 android android-intent android-activity
我有一个应用程序,如果应用程序在特定活动中崩溃,它将在其中一个中间父活动重新启动.
这对我来说是一个问题,因为我有一些输入的用户信息在崩溃时丢失.
有没有办法强制应用程序从崩溃重启后从启动器屏幕启动?
谢谢.
Pre*_*rem 13
android:clearTaskOnLaunch="true"将manifest.xml文件中的此标记添加到应始终启动的主活动中.
可能的原因为什么它不起作用
当应用程序崩溃时,它抛出一个Exception,我们需要处理Exception,否则我们不会得到预期的行为
尝试处理任何未捕获的异常并告诉系统该做什么.要实现此功能,请尝试以下步骤.
ApplicationClass的类uncaughtException你的Application子类.Activity,给你的Application班级打电话.Activity(根据您的要求).第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
}
}
Run Code Online (Sandbox Code Playgroud)
第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();
.............
}
}
Run Code Online (Sandbox Code Playgroud)
第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
}
Run Code Online (Sandbox Code Playgroud)
首先,App在您的AndroidManifest.xml和
android:name=".App"
android:clearTaskOnLaunch="true"
Run Code Online (Sandbox Code Playgroud)
then put this code in the App class
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 ");
}
});
}}
Run Code Online (Sandbox Code Playgroud)
也许没有办法做到这一点,但您可以对其进行标记,以便您知道该活动是通过用户操作启动的还是在崩溃后刚刚启动。
即当您启动父活动时,将一些内容传递到 startActivity 意图中。如果不存在,那么它是在崩溃后启动的。
| 归档时间: |
|
| 查看次数: |
19246 次 |
| 最近记录: |