Reputation: 10078
My activity has a strange behavior. I've override onBackPressed() method for perform some task before close activity, but i need to press twice back button for exit. What's wrong?
@Override
public void onBackPressed() {
super.onBackPressed();
String[] children = temp_directory.list();
for (int i = 0; i < children.length; i++) {
new File(temp_directory, children[i]).delete();
}
finish();
}
EDIT: i've noticed that when i press back button for first time, OnResume method is called.
EDIT TWO: if i remove all cleanup on files, and write :
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("TAG", "ondestroy");
}
at first back pressed, onresume is called. On second tap, ondestroy is called, but twice (log print message twice only after second back tap)
Upvotes: 0
Views: 4315
Reputation: 29762
I wrote some experimental code below that tries to duplicate the problem of taking too long to delete the files. What I found is that the activity exits right away, without delaying for the 10 seconds.
This proves that the call to super.onBackPressed()
would normally exit your activity - the code doesn't get to deleting the files.
public void onBackPressed()
{
// this call would exit the activity
super.onBackPressed();
// long delay - pretending to delete files - never runs
try
{
Thread.sleep(10000);
}
catch (InterruptedException e)
{
}
finish();
}
Update: @nickes points out that this is only true for the root activity. A second activity launched WILL experience the delay.
This different behaviour depending on how many activities are on the stack can be seen in the code for Activity.finish()
on grepcode here:
public void finish() {
if (mParent == null) {
int resultCode;
Intent resultData;
synchronized (this) {
resultCode = mResultCode;
resultData = mResultData;
}
if (Config.LOGV) Log.v(TAG, "Finishing self: token=" + mToken);
try {
if (ActivityManagerNative.getDefault()
.finishActivity(mToken, resultCode, resultData)) {
mFinished = true;
}
} catch (RemoteException e) {
// Empty
}
} else {
mParent.finishFromChild(this);
}
}
Update 2: @giozh removed all cleanup code, and still has the problem. So the clean-up code / delay is not the issue.
Try removing finish()
and move super.onBackPressed()
to the end:
@Override
public void onBackPressed() {
String[] children = temp_directory.list();
for (int i = 0; i < children.length; i++) {
new File(temp_directory, children[i]).delete();
}
super.onBackPressed();
}
Based on @nickes comment above, made me think it could be the superclass that prevents the activity from closing - so another option would just be to remove the call to super.onBackPressed()
.
Update: @giozh confirms that the superclass is a plain old vanilla Activity
, so this is not the solution.
Upvotes: 1
Reputation: 22512
It is because your cleanup takes too much time. You think that first back press doesn't work and press again. Try to do you cleanup async. And of course onDestroy()
is a lot more better place to do this.
Or you Activity is inherited from some class that overrides onBackPressed()
Upvotes: 1