Every android application is compose of several android activity. Let me demonstrate how activity works.
The above example is a simple demonstration of what is an Android Activity and how it is being implemented in android. Now every activity, Main Activity and Activity 2 in our example, have a life cycle. They are created, started and destroyed. Here is the complete life cycle of an Android Activity from Android developers documentation.
Android Life Cycle of an Activity
1.Application is first launched
Main Activity's onCreate() ->Main Activity's onStart() ->Main Activity's onResume() are called.2. Button is clicked opening Activity2.
Main Activity's onPause() -> Activity 2's onCreate() ->Activity 2's onStart() ->Activity 2's onResume() -> Main Activity's onStop() are called.Note: At onStop() the system has the right to call onDestroy() if memory is needed elsewhere.
2. Button is clicked opening Activity2.
Main Activity's onPause() -> Activity 2's onCreate() ->Activity 2's onStart() ->Activity 2's onResume() -> Main Activity's onStop() are called.Note: At onStop() the system has the right to call onDestroy() if memory is needed elsewhere.
Why is Activity 2's activities in between onPause() and onStop()?
According to android developers documentation when the activity is completely obscured by another activity onStop() is called. Therefore, for another activity to take place Activity 2's activities must start.
3. In Activity 2 a simple dialog box is opened.
Why nothing happened?
According to android developers documentation f an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations. What we implemented is a not an activity but a a dialog box.
4. In Activity2 Activity3 is opened implementing dialog theme.
Activity 2's onPause() -> Activity 3's onCreate() -> Activity 3's onStart() -> Activity 3's onResume()5.Back button is pressed closing Activity 3.
Activity 3's onPause() -> Activity 2's onResume() -> Activity 3's onStop() -> Activity 3's onDestroy()Why is onDestroy called?
According to android developers documentation if an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state. The system destroyed Activity 3 becuase probably it knows that when Activity3 is closed pressing the back button should reopen Main Activity.
6. Back button is pressed closing Activity 2.
Activity 2's onPause() -> Main Activity's onRestart() -> Main Activity's onStart() -> Main Activity's onResume() -> Activity 2's onStop() -> Activity 2's onDestroy()7. Home button is pressed.
Main Activity's onPause() -> Main Activity's onStop()Note: Pressing home button put the current activity at onStop so it can be resumed if reselected from running applications






No comments:
Post a Comment