Android Activity Components

Introduction: One of the four major components of Android, the medium for interaction between App and users.

Activity Lifecycle

In general the movement through an activity’s lifecycle looks like this:

1.onCreate()

Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity’s previously frozen state, if there was one.
Always followed by onStart().

2.onRestart()

Called after your activity has been stopped, prior to it being started again.Always followed by onStart()

3.onStart()

Called when the activity is becoming visible to the user.
Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

4.onResume()

Called when the activity will start interacting with the user. At this point your activity is at the top of its activity stack, with user input going to it.Always followed by onPause().

5.onPause()

Called when the activity loses foreground state, is no longer focusable or before transition to stopped/hidden or destroyed state. The activity is still visible to user, so it’s recommended to keep it visually active and continue updating the UI. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.
Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user.

6.onStop()

Called when the activity is no longer visible to the user. This may happen either because a new activity is being started on top, an existing one is being brought in front of this one, or this one is being destroyed. This is typically used to stop animations and refreshing the UI, etc.
Followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away.

7.onDestroy()

The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called Activity#finish on it), or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

Introduction to use

All activity classes must have a corresponding declaration in their package’s AndroidManifest.xml.

Activity jump between pages

  • startActivity(intent)
  • startActivityForResult(intent,request_code)

The startActivity(Intent) method is used to start a new activity, which will be placed at the top of the activity stack. It takes a single argument, an Intent, which describes the activity to be executed.

Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startActivityForResult(Intent, int) version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, Intent) method.

When an activity exits, it can call setResult(int) to return data back to its parent. It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER. In addition, it can optionally return back an Intent containing any additional data it wants. All of this information appears back on the parent’s Activity.onActivityResult(), along with the integer identifier it originally supplied.

If a child activity fails for any reason (such as crashing), the parent activity will receive a result with the code RESULT_CANCELED.

Activity ui state saving

  • Use onSaveInstanceState to save lightweight UI state, and Activity calls it in the onStop method.

Note: If you override onSaveInstanceState in Activity, you must call its parent class in order to save the uI state information to the view tree. Note: This method will not be called if the user closes the Activity manually or calls finish.

  • Page status information echo

Both the onCreate() and onRestoreInstanceState() callback methods return the state bundle object. Note: Because onCreate is a required life cycle function, if you process the state echo in the onCreate callback function, you need to determine whether the bundle object is In the case of nul, it will be echoed if it is not null. The onRestoreInstanceState() function is called by the system after the onStart callback function. In this method, there is no need to judge whether the bundle is null, because the system will only use this method if the state information has been saved.

Serialization and bundles

  1. For example, an app might set an alarm using the AlarmManager class, and use a custom Parcelable on the alarm intent. When the alarm goes off, the system modifies the intent’s Bundle of extras to add a repeat count. This modification can result in the system’s stripping the custom Parcelable from the extras. This stripping, in turn, can result in the app’s crashing when it receives the modified alarm intent, because the app expects to receive extra data that is no longer there.

  2. The Binder transaction buffer has a limited fixed size, currently 1MB, which is shared by all transactions in progress for the process. Since this limit is at the process level rather than at the per activity level, these transactions include all binder transactions in the app such as onSaveInstanceState, startActivity and any interaction with the system. When the size limit is exceeded, a TransactionTooLargeException is thrown.

  3. For the specific case of savedInstanceState, the amount of data should be kept small because the system process needs to hold on to the provided data for as long as the user can ever navigate back to that activity (even if the activity’s process is killed). We recommend that you keep saved state to less than 50k of data.

That’s all