Activity
- 1. What is Activity
- ()
- 3. Life cycle
- Normal life cycle
- Jump life cycle
- ()
- 5.[Activity startup mode](/qq_40384370/article/details/103264546)
- Close
- Startup process
- Communications
- Activity-Activity
- Activity-Fragment
- Activity-Service
1. What is Activity
Activity is the top four AndroidComponentsOne, Activity is a visual interface for user operations; it provides users with a serial port that can complete operation instructions. After creating the Activity, you need to call the setContentView() method to complete the display of the interface.
()
Can the setContentView method be called multiple times in the Activity?Tell me what happens when a second call setContentView is called at different times?
You can call it multiple times, for example, in the welcome interface, the background processing data display interface may have a delay. At this time, the setContentView() method can be called twice in the Activity to realize the welcome interface (this is also set in the usual advertisements on the app)
3.life cycle
Normal life cycle
onCreate: indicates that the Activity is being created and initialization operations are often performed, such as setViewContent interface resources and initialization data.
onStart: means that the Activity is being started, it is visible but has not appeared in the foreground, and it cannot interact with the user.
onResume: means that the Activity is getting focus. At this time, the Activity is visible and appears in the foreground, and can interact with the user.
onPause: means that the activity is stopping, and can be used for data storage, stop animation, etc.
onStop: Indicates that the activity and is about to stop, it can do slightly heavyweight recycling work, such as canceling the network connection and canceling the broadcast receiver.
onDestroy: means that the activity is about to stop, and the recycling work and resource release are often done.
Jump life cycle
->B
Activity A starts another method that will callback when Activity B starts: Activity A onPause() -> Activity B onCreate() -> onStart() -> onResume() -> Activity A's onStop(); If B is completely transparent, Activity A's onStop() will not be called in the end; if it is Dialog, the same is true for the latter case.
2. Switch horizontal and vertical screens
onPause
onSaveInstanceState // Here it can be used to save data by switching horizontally and vertically
onStop
onDestroy
onCreate
onStart
onRestoreInstanceState// Here it can be used to switch horizontally and vertically.
onResume
Resource adaptation method:
①Adaptive life cycle, save recovery state, dynamic adaptive layout
② Set it in the middle to prevent the activity from retracing the life cycle to save the state, and then use onConfigurationChanged to adaptively layout
android:configChanges=orientation|screenSize
- 1
③Side and vertical screen switching is prohibited
3. The foreground switches to the background, and then the life cycle of the activity when you return to the foreground.
Click the home button to return to the desktop –>onPause() –>onStop()
When you return to the original Activity again –>onRestart() –>onStart() –>onResume()
4. What is the life cycle of the Activity when pulling down the status bar?
First, the notification bar is pulled down a little bit to match the general description "Activity is partially blocked" - onPause()
Then, after the notification bar completely falls, "Activity is blocked" - onStop()
()
onSaveInstanceState() may not necessarily be called, so it is not recommended to save persistent data. If it is persistent data, it is recommended to save it in onPause(). onSaveInstanceState() is only used to save transient data, such as control status, member variables, etc. It is not recommended to perform time-consuming operations in onSaveInstanceState() and onPause().
5.Activity startup mode
Close
Close all: Create a class, put it into List when onCreat(), traverse List, and call finish
Close the specified: Set a tag for each startup activity, and close the specified activity according to the location where the tag and the collection can reach.
Startup process
Communications
Activity-Activity
1. Set global variables
:The data size is limited. Data below 512k can be transmitted normally. 512k-1024k will crash and above 1024k will report an error.
Explicit start: directly specify the activity class name to jump without filtering, it is efficient, suitable for jumping different activities in the same application
Implicit startup: It requires filtering, which is relatively time-consuming, but all matching applications can be found. Suitable for Activity jumps between different applications.
Implicit startup will find the matching application and prompt the user to select the opening method. If multiple components are successfully matched, the user will make the selection in the form of a dialog box list.
: Store in A and read in B
Activity-Fragment
:Coupling is not conducive to Fragment multiplexing. If you use too much Handler, it will be generated.Memory leak。
2. Broadcasting (extravagant material)
3.EventBus
4. Interface getActivity/getSupportFragmentManager()
Activity-Service
1. Add Intent to startService and stopService methods for communication
2. Create ServiceConnection() anonymous class, override the onServiceConnencted() method and onServiceDisconnected() method
Note: Any service is common throughout the application scope. MyService can not only be bound to MainActivity, but also to any activity. After binding, get the same DownloadBinder instance.
+Handler