1

I have a simple activity with an image view and a simple menuItem which is used for came back in the previus activity.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case android.R.id.home:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

I just realized that if I press the back button for coming back in the previous activity, android do it really fast, but pressing the menu item above, it waste a second. why?

1 Answer 1

1

The default way of doing a return back is

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case android.R.id.home:
            return super.onOptionsItemSelected(item);
    }
}

By calling finish() you schedule the current activity for destruction, i.e. to call onDestroy(). This does some clean-up and thus produces an overhead and you experience therewith some latency. So, if you don't really have to use finish() there (e.g. to close some dialogs or cursors), just omit it and use the provided default solution.

Hope this helps!

2
  • I try.. this doesn't come back in the previus activity! just nothing happens, I post my code.. the activity is open from a normal intent.. @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case android.R.id.home: return super.onOptionsItemSelected(item); case R.id.share_image: //TODO return true; default: return super.onOptionsItemSelected(item); } } Dec 30, 2017 at 14:38
  • You have to provide the logical parent of your activity in the manifest file. The attribute for that is android:parentActivityName.
    – Dimitar
    Dec 30, 2017 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.