Do I need to implement the methods onCreate (), onResume, etc. In every operation I write?

I started exploring the android world just a few days ago, and I do this with the help of Mario Tsechner’s book, “Getting Android Games.”

I could have a lot of questions about the platform and a few things that I have seen so far, but I know it will get better. All I want to ask at the moment about activity: I have seen the life cycle of activity. I know that actions are something like screens. What I do not know is whether I need to specify the methods onCreate (), onResume (), etc. In every code I encode.

+4
source share
6 answers

As far as I know, onCreate() is required, and other methods depend on how you use the activity

+4
source

The entire activity lifecycle occurs between the first call to onCreate (Bundle) to one final call to onDestroy (). The activity will configure the "global" state in onCreate () and release all remaining resources in onDestroy (). Therefore, onCreate (Bundle) must be there in action. Using onResume () depends on your application requirement. for more details go to http://developer.android.com/reference/android/app/Activity.html

+1
source

Welcome to the world of Android.

In general, it’s good practice to use all the methods, such as onPause (), onResume (), but when you create an Android program, you usually only need to work out the onCreate () method for the actions.

Besides onCreate and forgiveness, if my terminology is incorrect, other methods follow by default if you do not override them. Therefore, if you need the application to do something specific when it is paused, it would be nice to add your version of onPause (), otherwise you can leave it aside.

+1
source

It is not necessary to indicate all of these methods or any of them. It depends on what type of implementation you want.

Example I created my activity (A) because it extends Activity I, but does not override any methods like onCreate (), but I created some variables and created some of the methods. Suppose I created a second activity where I want to customize some kind of performance. I used the onCreate () method. If I want the variables and methods that I defined in the AI ​​activity, they can get these variables and methods. If I write class B extends A

Therefore, it is not necessary to use all these methods from activity. If you have not written your own implementation, then the default implementation will be implemented.

0
source

The short answer will be NO

You do not need to specify in the code of each Activity onCreate and so on. In any case, the parent Activity will be onCreate

But the long answer says: good practice does not depend on implicit / invisible code, but in order to have code under your control (even if it is dummy). I used to encode all onCreate / onDestroy etc. In this way:

 public static final boolean DEBUG=true; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(DEBUG) Log.d(TAG, "Creating "+this.toString()); } 
0
source

You must write the onCreate() method, overriding it from the Activity base class to set the view. The view should be generated here itself using the setContentView() method in the onCreate() method. As for onResume() , onPause() and other methods, it is not necessary to write them, but they are useful when you need to achieve certain functionality.

Also, as a newbie, check out Table 1 in this document: http://developer.android.com/guide/topics/fundamentals/activities.html

0
source

All Articles