Why is it that you need to call the super class in the android lifecycle? For example, in onCreate you need to call super.onCreate, or onDestroy super.onDestroy.
It makes sure that any relevant lifecycle management code across the full class hierarchy gets invoked.
If you have MyBaseActivity that extends Activity, and MySpecificActivity that extends MyBaseActivity, calling through to the lifecycle methods in the superclass at each level means MyBaseActivity will still be able to respond to lifecycle events.
Related
I am new to android development.
So I have a question about activities.
On Google developer, they say you have to make a class which is inheriting from the class Activity.
So far so good. Now I am thinking an Activity is a class, which leads to my major problem.
So when activity is a class how can it invoke all the live cycle methods.
In Java, I learnt there must be an object of a class to invoke methods and actions. So when activity is not an object but a class, how is this possible.
Kindest regards
Jan
When a user starts your app the android operating system will instantiate an object based on your class which must be a subclass of Activity.
The operating system will use that object and call the lifecycle hooks (e.g. onCreate, onResume, ...) which you implented in your activity subclass when one of the related lifecycle events happens.
Here it is clearly mentioned under CREATING AN ACTIVITY header, that system calls the callback methods, based on which event has occurred. Suppose user is interacting with your activity, then using the object created by the OS for that activity, all the callback methods will be executed. Please refer.
I want to use BaseGameActivity from the games examples:
https://github.com/playgameservices/android-samples/blob/master/BaseGameUtils/src/com/google/example/games/basegameutils/BaseGameActivity.java
but my game activity has to extend from another framework's activity class. Is there an implementation of BaseGameActivity factored out into a separate class so I don't have to have my activity inherit from it?
Thanks
BaseGameActivity is a pretty simple wrapper around GameHelper, which is the object that really does all the work. If you can't derive from BaseGameActivity, simply use GameHelper directly. The implementation of the BaseGameActivity class can serve as an example of how to hook up the GameHelper methods to your Activity's lifecycle.
In summary:
in your Activity's onCreate, create the GameHelper object.
from your Activity's onStart, call GameHelper's onStart
from your Activity's onStop, call GameHelper's onStop.
from your Activity's onActivityResult, call GameHelper's onActivityResult.
implement the GameHelperListener interface methods
There's no requirement to use exactly that BaseGameActivity. Create your own BaseGameActivity based on that source code but instead of
public abstract class BaseGameActivity extends FragmentActivity implements
GameHelper.GameHelperListener
put
public abstract class BaseGameActivity extends YourFrameworksActivity implements
GameHelper.GameHelperListener
I have created an Activity which allows a user to create a page. This PageActivity extends an Activity. Can I create a class called Update PageActivity that extends PageActivity?
Yes, you can, as long as you did not declare PageActivity a final you may extend it.
Of course you can.
Activity is just like any other Java Class.
Make sure you override the relevant Activity lifecycle functions - onCreate, onResume, etc...
I've derived a class from IntentService and I'm wondering if it is necessary to call through the super class onCreate and onDestroy methods when overriding these methods in my implementation as it happens when you override such methods in an Activity.. If it is necessary, do these invocation need to be the first thing we do in the overriding method? In the Activity documentation they are very clear about that, while in the Service or IntentService docs I can't find anything specific.
I'm wondering if it is necessary to call through the super class onCreate and onDestroy methods when overriding these methods in my implementation as it happens when you override such methods in an Activity.
Absolutely. Those methods are implemented on IntentService; if you fail to call through to them, your service simply will not work.
If it is necessary, do these invocation need to be the first thing we do in the overriding method?
I would recommend calling super.onCreate() as the first thing that you do in your implementation of onCreate(), and calling super.onDestroy() as the last thing that you do in your implementation of onDestroy().
I have googled but I have not found an equivalent flowchart showing the life cycle of PreferenceActivities in android. Does such a flowchart exist? The one showing basic activity lifecycle was a big help getting to understand how activities work.
The PreferenceActivity class extends the Activity class¹, hence the same lifecycle applies.
A PreferenceActivity just does some things for you in the relevant lifecycle methods, see the source. Since you have to call the relevant superclass methods anyway, there isn't any major difference (e.g. you have to call super.onPause() when you override onPause()).
¹ it extends a ListActivity, which then extends the Activity class