Interview Questions for Xamarin Android
Activity LifeCycle?
The following diagram illustrates the states an Activity can go through during its lifetime:
The following diagram illustrates these methods in relationship to the Activity Lifecycle:
OnCreate
This is the first method to be called when an activity is created.
OnCreate
is always overridden to perform any startup initializations that may be required by an Activity such as:- Creating views
- Initializing variables
- Binding static data to lists
OnStart
This method is always called by the system after
OnCreate
is finished. Activities may override this method if they need to perform any specific tasks right before an activity becomes visible such as refreshing current values of views within the activity. Android will call OnResume
immediately after this method.
OnResume
The system calls this method when the Activity is ready to start interacting with the user. Activities should override this method to perform tasks such as:
- Ramping up frame rates (a common task in game building)
- Starting animations
- Listening for GPS updates
- Display any relevant alerts or dialogs
- Wire up external event handlers
OnPause
This method is called when the system is about to put the activity into the background or when the activity becomes partially obscured. Activities should override this method if they need to:
- Commit unsaved changes to persistent data
- Destroy or clean up other objects consuming resources
- Ramp down frame rates and pausing animations
- Unregister external event handlers or notification handlers (i.e. those that are tied to a service). This must be done to prevent Activity memory leaks.
- Likewise, if the Activity has displayed any dialogs or alerts, they must be cleaned up with the
.Dismiss()
method.
There are two possible lifecycle methods that will be called after
OnPause
:OnResume
will be called if the Activity is to be returned to the foreground.OnStop
will be called if the Activity is being placed in the background.
OnStop
This method is called when the activity is no longer visible to the user. This happens when one of the following occurs:
- A new activity is being started and is covering up this activity.
- An existing activity is being brought to the foreground.
- The activity is being destroyed.
OnDestroy
This is the final method that is called on an Activity instance before it's destroyed and completely removed from memory. There will be no lifecycle methods called after the Activity has been destroyed.
OnRestart
This method is called after your activity has been stopped, prior to it being started again. A good example of this would be when the user presses the home button while on an activity in the application. When this happens
OnPause
and then OnStop
methods are called, and the Activity is moved to the background but is not destroyed. If the user were then to restore the application by using the task manager or a similar application, Android will call the OnRestart
method of the activity.
Explain the Activity lifecycle when it goes from Activity A to Activity B and vice versa
When the app first starts, the output window displays the state changes of Activity A:
[ActivityLifecycle.MainActivity] Activity A - OnCreate
[ActivityLifecycle.MainActivity] Activity A - OnStart
[ActivityLifecycle.MainActivity] Activity A - OnResume
When we click the Start Activity B button, we see Activity A pause and stop while Activity B goes through its state changes:
[ActivityLifecycle.MainActivity] Activity A - OnPause
[ActivityLifecycle.SecondActivity] Activity B - OnCreate
[ActivityLifecycle.SecondActivity] Activity B - OnStart
[ActivityLifecycle.SecondActivity] Activity B - OnResume
[ActivityLifecycle.MainActivity] Activity A - OnStop
When we click the Back button, Activity B is destroyed and Activity A is resumed:
[ActivityLifecycle.SecondActivity] Activity B - OnPause
[ActivityLifecycle.MainActivity] Activity A - OnRestart
[ActivityLifecycle.MainActivity] Activity A - OnStart
[ActivityLifecycle.MainActivity] Activity A - OnResume
[ActivityLifecycle.SecondActivity] Activity B - OnStop
[ActivityLifecycle.SecondActivity] Activity B - OnDestroy
What is the purpose of WebView?
Note : I'll be putting some more questions in few days.
WebView
allows you to create your own window for viewing web pages (or even develop a complete browser).Note : I'll be putting some more questions in few days.
Comments
Post a Comment