问题
To preserve resources and prevent memory leaks, I am calling finish() in onPause event whenever app is going from one activity to another. I think it works fine, but when i try to rotate screen, app is crashing - error is "Duplicate finish request" How I can prevent this, is there way in onPause event to detect if app is going to the next activity or just changing orientation? Is there better method for preserving memory then using finish?
Thanks for help!
回答1:
Just a shot in the dark here, but are you calling super.onPause() in your onPause implementation?
回答2:
You could try the Intent.FLAG_ACTIVITY_NO_HISTORY flag. This will finish your activities as soon as new activity appears in front of them.
You can put this into an intent when you start an activity:
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
or you can set it in your manifest so that your activity always carries this flag:
android:noHistory="true"
as an attribute in that activity's <activity ... />
tag.
This way, you can avoid trying to call finish
in onPause
. I think this is better than trying to figure out exactly when the framework will call finish
itself.
回答3:
Test if you're Activity is already finishing with this.isFinishing() (where this is your activity) ?
来源:https://stackoverflow.com/questions/5328210/activity-calling-finish-in-onpause-crashes-during-orientation-change