Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnPause(), OnResume() and OnDestroy

I have developed a small app which uses several different classes to allow the user to add,remove and view certain data. I use an arrayList to store entries and this has its own class which has methods linked to the array so i can access the array from all of my classes.

I want to introduce the above methods to optimise my code and was wondering if I could stick them in one class hopefully the same application class I have my array in and there just define them where they will be getting used, this will save me having to write the same piece of code in several classes, is this possible?

Thanks

like image 245
Melevin Mandana Avatar asked Dec 02 '22 00:12

Melevin Mandana


2 Answers

Examples when onPause, onResume, onDestroy are called by Android.

OnPause()

OnPause() is called when the user receives an event like a call or a text message, when onPause() is called the Activity may be partially or completely hidden.

You would want to save user data in onPause, in case he hits back button without saving the data explicitly.

OnResume()

OnResume() is called when the user resumes his Activity which he left a while ago, say he presses home button and then comes back to app, onResume() is called.

You can do the network related updates here or anything of this sort in onResume.

OnDestroy()

OnDestroy is called when the Activity is being destroyed either by the system, or by the user, say by hitting back, until the app exits.

Its compulsory that you save any user data that you want to persist in onDestroy(), because the system will not do it for you.

Having said that, I must say you that you cannot call any of the user defined method's in onPause() and onDestroy() as they are called under certain circumstances and are not meant to do any operation other than what I specified above.

When the Activity starts, onCreate() is called which sets up your screen followed by onResume(), you can however use onResume to do what you want, but it is highly recommended that you create another class to carry on such operations, which may sometimes be data intense. Finally call those methods you created in another class in your onCreate() and onResume() methods within the Activity.

like image 198
Arif Nadeem Avatar answered Dec 04 '22 08:12

Arif Nadeem


Better to an base activity which contains your code for override methods and then you can extend it your activities instead of activity.

So all the activities which extend baseactivity will be reflected with three methods.

public class BaseActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }
   @Override
protected void onPause(){
    super.onPause();

}
@Override
protected void onResume(){
    super.onResume();

}
@Override
protected void onDestroy(){
    super.onDestroy();

}

}

Note what task you want to perform in the above three method? are they same in all activiies then only use the above.

like image 28
Shankar Agarwal Avatar answered Dec 04 '22 08:12

Shankar Agarwal