Android studio case-test activity life cycle
table of Contents
One, recognize activity
1. Know the life cycle of activity
Seven methods and four states of activity life cycle:
- When it is in the foreground of the screen (at the top of the current task stack), it is in an active state that is visible and interacts with the user, called
活动状态或者运行状态
; - When the activity is covered by another transparent activity or dialog style activity. The state at this time is called
暂停状态(paused)
. It is still connected to the window manager, and the system continues to maintain its internal state, so it is still visible, but it has lost its focus and cannot interact with the user; - If an activity is completely covered by another activity
停止状态(stoped)
, it is called , and it still maintains all state and member information. But it can no longer be seen, so its window is hidden, when the system memory needs to be used in other places, the stopped activity will be forcibly terminated; - When the activity is killed and recycled by the system or is not started
断开状态(killed)
(if an activity is paused) or stopped, the system can delete the activity from the memory. The Android system deletes the activity in two ways, either by requiring the activity to end, or Terminate its process directly. When the activity is displayed to the user this time, it must restart and reset the previous state).
activity的状态转换时会选择性的调用以下方法:
void onCreat(Bundle saveIntanceState)
void onStart()
void onRestart()
void onResume()
void onPause()
void onStop()
void onDestroy()
重写activity中的几个方法:
public class Activity extends ApplicationContext{
void onCreat(Bundle saveIntanceState);
void onStart();
void onRestart();
void onResume();
void onPause();
void onStop();
void onDestroy();
}
2. The life cycle process of an activity
Four states
- Running
- Paused state (paused)
- Stopped
- Disconnected state (killed)
Seven methods:
1, onCreat
2, onStart
3, onRestart
4, onResume
5, onPause
6, onStop
7, onDestroy
flow chart
Second, the test life cycle
1. Download the help document
I found it in the SDK manager documentation for Android SDK
(the description is omitted, see the picture to operate), but I
don’t find it. I know through Baidu that the new version of Android studio is different from the previous version
2. The life cycle of the activity jump process
- View the life cycle of two activities by starting another activity
- Bactivity exists in the form of a dialog box
<activity android:name=".demoActivity"
android:theme="@style/Base.Theme.Appcompat.Diolog">
</activity>
3. The life cycle of switching between horizontal and vertical screens
When switching between horizontal and vertical screens, the life cycle of the activity is as follows; the activity is recreated after being destroyed
Requirement 1
By default (there is no android:configChanges attribute);
- Vertical screen cut horizontal screen, after destroying the current activity, create a new activity instance;
- Cut the horizontal screen to the vertical screen. After destroying the current activity, create a new activity instance. The new activity instance will be destroyed soon, and then a new activity instance will be created. If you only want to create one instance, you can configure
android:configChangs="orientation"
In order to prevent the discovery of this situation, you need to add attributes in the activity configuration xml file
android:configChanges="keyboardHidden|orientation|screenSize"
Requirement 2 is by
default (there is no android:configChanges attribute);
- Vertical screen cut horizontal screen, create a new activity instance after destroying the current activity
- Cut the horizontal screen to the vertical screen. After destroying the current activity, create a new activity instance. The new activity instance will be destroyed soon, and then a new activity instance will be created. If you only want to create one instance, you can configure
android:configChangs="orientation"
android:screenOrientation="landspace"//横向模式
android:screenOrientation="portrait"//竖向模式
Three, reference materials
The life cycle of the activity when switching between horizontal and vertical screens .