OnPause (), OnResume () and OnDestroy

I developed a small application that uses several different classes to allow the user to add, delete, and view certain data. I use arrayList to store records, and this has its own class that has methods associated with the array, so I can access the array from all my classes.

I want to introduce the above methods for optimizing my code and I was wondering if I can embed them in one class, I hope the same application class in which there is my array, and there just determine where they will be used, it will save I need to write the same piece of code in multiple classes, is this possible?

thank

+3
source share
4 answers

Better is the base activity that contains your code to override the methods, and then you can extend its activity, not the activity.

Thus, all actions that expand the basic activity will be reflected in three ways.

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();

}

}

Pay attention to what task you want to accomplish in the above three method? they are the same in all actions, and then use only the above.

+2
source

Examples of when onPause, onResume, onDestroycausing Android.

Onpause ()

OnPause()triggered when the user receives an event similar to a call or text message, when OnPause()called Activitycan be partially or completely hidden.

onPause, "", .

OnResume()

OnResume() , Activity, , , "", , OnResume().

- onResume.

OnDestroy()

onDestroy , Activity , , , , .

, onDestroy(), .

, , - onPause() onDestroy(), - , , .

, onCreate(), , OnResume(), onResume , , , . , onCreate() OnResume() Activity.

+12

, , , singleton. . . MyObject = Singleton.getInstance();

+2

well, you can create a BaseActivity class that extends the Activity, overrides the methods you want to override, and instead extend the Activity in all your other actions, extend the BaseActivity instead.

0
source

All Articles