Activity class in Android application development
In previous article we discussed about android activity class and task and task back stack, activity life cycle and so on. In this article we will discuss methods of activity class.
This method is called when an activity is created. This method sets up the initial state. It carry out the following four functions.
- Call super.onCreate() so android can do some of its own initializations.
- It will set the activity’s content view. Which tell android what the activity’s UI should be.
- Retail any references to UI views as necessary.
- Configure the views as necessary
As you can see the image above for OnCreate method. First it calls super.onCreate method. Then it sets the content view. After setting the content view it initializes the UI elements and then link the UI elements to the code as button.setOnClickListener.
OnRestart is another activity life cycle method and is called when an activity is stopped and is about to restart. We can call any typical actions or special processing that needed only after having been sopped.
Onstart is called when an activity is about to be visible. We can start when-visible behaviors.
OnResume method is called when activity is visible and about to start interacting with user. We start foreground-only behaviors. things like starting animations or starting a background sound track.
OnPause Method is called when activity is about to loose focus or focus is about to switch to another activity. Typical actions taken on this method are shutdown-foreground only behaviors, like killing animations or save any persistent state that user has added.
Activity is no longer visible to the user. At this point it is possible that activity may be restarted later. This method may not be called when android kills the activity, may be because of low memory.
OnDestroy method is called when activity is about to be destroyed. But it may not be called when android kills the activity.
- To start an activity We need to Create an Intent Object specifying the activity to start.
- Then we need to pass the newly created Intent to methods like startActivity() or startActivityForResult()
- StartActivity will start an activity but startActivityForResult will also start activity but it will do so with an expectation that the started activity will provide a result back to the calling activity and it invokes a callback method when called activity finishes to return a result.
Started activity can set its result by calling Activity.SetResult.
Result has codes like
or we can add custom result codes.
When an Activity calls startActivityForResult(), it will eventually receive a callback to onActivityResult() method. and when one Activity wants to start another activity it creates an Object that specifies which Activity it wants to start and type of this Object is Intent Object.
Handling Configuration Changes
A device’s configuration refers to device and application characteristics related to application resources, things like languages used, screen size, keyboard availability and device orientation. These device orientations can change at run time. On configuration change android kills the current activity and restarts it. Since configuration changes can happen frequently so we should make sure that activity restarting is fast.
Suppose if we have an Android Device and we moved a device from portrait to landscape mode and then again from landscape to portrait mode that will cause the current activity to be killed and restarted twice. So if you start up code is slow than that will effect user’s experience with your application. So to improve the speed of configuration changes android allow you to do two things.
- When configuration changes are occurring and arbitrary java object can be created that caches important state information.
- Configuration can be handled manually, avoiding the whole restart and shut down sequence.
Let us discuss these methods one by one
Retaining an Object
The hard to recompute data can be cached to speed up handling of configuration changes.
We can do this by overriding
onRetainNonConfigurationInstance() method to build and return configuration object and will be called between onStop() and onDestroy(). The object that is build and returned by onRetainNonConfigurationInstance() method cab be retrieved when the activity is recreated, by calling getLastNonConfigurationInstance () during onCreate() to recover retained object.
Important Note: These methods have been deprecated in favor of methods in Fragment class.
We can declare the specific changes that our application will handle in AndroidMenifist.xml file. When we handle changes manually Activity’s onConfigurationChanged() method is called, pass a configuration object specifying the new device orientation.