2

When pressing the backbutton I want that, whatever screen will be loading, the onCreate() method will be executed. I want this because the screens must be refreshing when navigating through the app.

Do I need to override the back button method?

if(keyCode == KeyEvent.KEYCODE_BACK)
{

    }
user1264255
  • 305
  • 1
  • 5
  • 13
  • 1
    Calling the OnCreate() when the back button is pressed sounds like really bad practice. Anyway you can still override the OnBackPressed method – Ika May 02 '12 at 08:24

4 Answers4

16

When you press the back button, the onResume() method is called, so instead of using onCreate(), use this and do whatever you need to do for refreshing the activity.

lambda
  • 3,295
  • 1
  • 26
  • 32
Finuka
  • 730
  • 7
  • 15
  • That's the right answer. See http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle – shkschneider May 02 '12 at 08:26
  • 2
    To get to know the Activity lifecycle a bit better I also suggest that you display a toast everytime the different methods of an Activity are triggered.. so in onResume() you Toast.makeText(this, "onResume()", Toast.LENGTH_SHORT).show(); .. same in onStop() and onCreate(), onStart() etc :) – DecodeGnome May 02 '12 at 08:36
2

You approach is wrong. onCreate() will only be called when the Activity is created. But everytime the Activity comes to the front the Method onResume will be called, this Method will also be called on the first start.
Look at the Activity Lifecycle for further info: http://fs01.androidpit.info/wiki/de/b/Beginners_Workshop_Activity_LC.png

Thommy
  • 5,070
  • 2
  • 28
  • 51
1

When an activity is coming from background its onResume() method will be called. You should update your UI there.

sachy
  • 739
  • 7
  • 13
0

f you want execute anything after the back button you have to override it. Can you just refresh some part of the activity, not the whole one?

goodm
  • 7,275
  • 6
  • 31
  • 55