问题
I have a large android app and recently I am seeing that on any uncaught exception, the exception is shown in the DDMS Logcat and following that the calling parent activity gets relaunched (onCreate get's called). Ideally the application should just exit.
We are using ARCA crash application reporting, but commented out that and still see the same activity getting relaunched. I am calling the startActivityForResults and the android manifest has android:finishOnTaskLaunch true for the activity as well as the subactivity.
Any pointers on what could be causing the activity relaunch on the exception?
回答1:
I believe this is the expected behavior of force closes. The user gets informed of the exception, when they acknowledge it the system tries to get them back as close as possible to their current state, i.e., the last activity that worked.
I'm not sure why this behavior wouldn't be desired, but you probably need to come up with your own method of recognizing that the activity was restarted because of a crash and immediately exit in onCreate.
Edit: I just put together a test app:
public class Activity1 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.button);
button.setText("test");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Activity1.this, Activity2.class);
startActivityForResult(intent, 0);
}
});
}
}
public class Activity2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.button);
button.setText("test2");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
throw new RuntimeException();
}
});
}
}
When you push the button in Test2, the exception is thrown. Android shows a force close dialog, and when you click OK in the dialog, it takes you back to Test. That is what I meant by the default behavior.
The best way to deal with this is to fix your app so that it never throws an exception.
As a last resort, you could add a handler for dealing with uncaught exceptions: uncaughtExceptionHandler. This prevents the force close dialog from being shown in the first place, so you can do as you like.
来源:https://stackoverflow.com/questions/4466529/uncaught-exception-relaunching-the-calling-activity