Reputation: 25
I am new to android. I have a question about onResume(). How to make my activity rerun code in onCreate() after back from the 2nd activity?(Example i have updated some data in activity2 and wish to refresh activity1 to run code in onCreate() after back from activity2) Anyway better practice instead of retype code in onCreate() to onResume(). Sorry for my bad English grammar and thank in advance.
Upvotes: 0
Views: 83
Reputation: 1204
Extract the part you wish to run in onResume()
and onCreate()
into a separate function, then call that function both in onCreate()
and onResume()
.
protected void onCreate(Bundle b){
//Code specific to onCreate call
//...
//Invoke the common function
commonFunction();
}
protected void onResume(){
//Invoke the common function here as well
commonFunction();
}
private void commonFunction(){
//piece of code you want to execute in both onCreate and onResume
//...
}
Upvotes: 1