user1264255
user1264255

Reputation: 305

Android: how to use OnCreate() when pressing backbutton

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)
{

    }

Upvotes: 2

Views: 5490

Answers (4)

Finuka
Finuka

Reputation: 730

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.

Upvotes: 16

Thommy
Thommy

Reputation: 5407

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

Upvotes: 2

goodm
goodm

Reputation: 7295

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?

Upvotes: 0

sachy
sachy

Reputation: 789

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

Upvotes: 1

Related Questions