0

I know that when we press back button, the default function onBackPressed() is called.

http://developer.android.com/reference/android/app/Activity.html#onBackPressed()

This doc say that "The default implementation simply finishes the current activity". What does it means, does it mean the default onBackPressed() include the function finish()?

What is the implementation inside finish()? Is that onDestroy()?

Keith Mak
  • 453
  • 1
  • 5
  • 16
  • The Android developer documentation explains the Android lifecycle model. It shows you what finish() does and when onDestroy() is called. You read that documentation, and you'll learn what exactly the back button does. Often, you don't want the default behavior of the back button. When you create an app, you should determine what the most logical behavior of the back button is, the behavior that the user would expect. – Christine Aug 13 '14 at 11:07

1 Answers1

5

This is how implementation looks like:

public void onBackPressed() {
    if (!mFragments.popBackStackImmediate()) {
        finish();
    }
}

Of course on Android 2.3 and lower it's like this:

public void onBackPressed() {
     finish();
}

There was no Fragments API.


You can always check sources yourself in your IDE or on the web

Damian Petla
  • 8,963
  • 5
  • 44
  • 47
  • I wanna ask a question, what's inside **finish()**? – Keith Mak Aug 13 '14 at 11:15
  • in finish() it close the current activity means it destroy current activity and it shows previously pausedActivity or previousely opened Activity which is in queue – Android is everything for me Aug 13 '14 at 11:19
  • The answer below said that "Activity is not destroyed completely when back button is pressed", is that true? – Keith Mak Aug 13 '14 at 11:24
  • When finish() is called system go through lifecycle methods and eventually call onDestroy() for your Activity. Previously visible Activity is shown, it does not have to be your app. @shylendra is wrong saying "Activity is not destroyed completely when back button is pressed" – Damian Petla Aug 13 '14 at 11:47
  • Regarding "what's inside fnish()", as I wrote in my answer check the source or the web e.g. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.4_r1/android/app/Activity.java#Activity.finish%28%29 You have to learn making use of the source, it's a very good practice to improve Android programming. – Damian Petla Aug 13 '14 at 11:54
  • You are welcome. If it is enough for you then please chose your correct answer. – Damian Petla Aug 13 '14 at 12:39