Life Cycle Sequence of Activity A Starting Activity B
Opening Method
btClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
Log.i("xx","intent Before and before implementation");
startActivity(intent);
Log.i("xx","intent After execution");
}
});
Add finish() method
Button btClick = (Button) findViewById(R.id.bt_click);
btClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
Log.i("xx","intent Before and before implementation");
startActivity(intent);
finish();
Log.i("xx","intent After execution");
// finish(); the same is true here, finish() is not implemented immediately, it is in accordance with the life cycle.
}
});
Several good articles about Activity:
1.Deep Understanding of Activity's Life Cycle - A Brief Book
2.Android development: 5 minutes parsing Activity & Fragment life cycle - brief book
3.Preservation and Recovery of Activity Data in Android-A Brief Book
On the Author's Views in the Third Activity
When I haven't taught Android by myself, playing with some Apps raises a question, such as I typed a lot of text into an input box that I didn't submit or save. At this point came a phone call. If the text in the input box disappeared when I returned, I might have smashed the phone. So did Android developers do this operation to save data?
However, it is not necessary, because Android View itself implements the onSave InstanceState method, and the controls themselves have the ability to save and restore temporary data.
Author: MeloDev
Source link: http://www.jianshu.com/p/6622434511f7
Personal understanding:
In fact, View controls such as EditText have the function of temporarily saving temporary data (in which the video is Home returning to the desktop, not by pressing the return key) - here for the onDestroy() method, if you go onDestroy() method, you need to save the data yourself.
Here, I summarize my personal understanding of data access:
- Temporary data is saved and restored using onSave InstanceState, and permanent data is saved using onPause method.
- 1. Because the onSaveInstanceState() method is implemented inside View components such as EditText and TextView, which has some temporary and non-permanent data storage and recovery. It is not necessary to consider not going onDestroy() method, such as Home returning to the desktop and returning after receiving a phone call (excluding the case of inadequate memory being killed, as mentioned below), the View component will be saved. Data
-
2. In the case of inadequate memory being killed (in onPause() or onStop() method being killed, i.e. abnormal death), the system will call the onSaveInstanceState() method of Activity, so we need to rewrite this method here to save data, and then restore it in onCreate() method.
PS:Activity's onSave Instance State (Bundle) method will walk when you press the Home key and lock the screen (note that the name of this method onSave Instance State (Bundle, Persistable Bundle) will not go. In fact, I personally think that onSave Instance State (Bundle) is only used for fear that memory is not enough to be killed.
//Add the following code to MainActivity to save temporary data:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
String tempData = "Something you just typed";
outState.putString("data_key", tempData);
}
/*The data has been saved, so where should we restore it? Careful you
It may have been discovered long ago that the onCreate() method we've been using actually has a Bundle type as well.
Parameters. This parameter is null in general, but it works well before the activity is reclaimed by the system.
If the onSaveInstanceState() method is used to save the data, this parameter will take the previous one.
All the data saved, we only need to use the corresponding value method to extract the data.
Modify the onCreate() method of MainActivity as follows:*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
String tempData = savedInstanceState.getString("data_key");
Log.d(TAG, tempData);
}
...
}
- 3. Permanent data is saved using onPause method. We can do some lightweight data storage and initialization work, which is not too time-consuming, because only when one Activity completes the onPause method, another Activity will start, and android specifies that if onPause does not complete within 500 ms or 0.5 seconds, it will be forced to close the Activity. Y.
- 4. For the heavyweight, we have not found a good way yet!!!
Life Cycle Sequence for Starting Fragment s in Activity
Source code: On Exploring Avctivity and Fragment's Life Cycle Source-CSDN Download
1. fragment defined in MainActivity
<fragment
android:id="@+id/top_fragment"
android:name="com.example.yueyue.myapplication.RightFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
The lifecycle of execution is as follows
PS: The last two methods worth noting here are:
I/xx: RightFragment onStart...
I/xx: MainActivity onStart
I/xx: MainActivity onResume
I/xx: RightFragment onResume...
2. Adding frament dynamically
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("xx",TAG+" onCreate");
initView();
}
//onCreate calls initView method in MainActivity
private void initView() {
fr_container = (FrameLayout) findViewById(R.id.fr_container);
bt_click = (Button) findViewById(R.id.bt_click);
bt_click.setOnClickListener(this);
//MainActivity left on Resume, and RightFragment left on Resume.
relaceFrgment(new RightFragment());
}
//Open a Fragment
private void relaceFrgment(Fragment fragment) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction tr = fm.beginTransaction();
tr.replace(R.id.fr_container, fragment);
// tr.commitAllowingStateLoss();
tr.commit();
}
PS: The last two methods are worth noting here (the methods here are not worth referring to, just want to draw the following summary):
Summary: Whether fragment s are added dynamically or statically, the order is
RightFragment onStart -> MainActivity onStart -> MainActivity onResume -> RightFragment onResume
3. The Life Cycle Method of Replacing Frgament with Frgament
private static int type = 0;
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_click://Open Fragment with Button
relaceFrgment(type++);
break;
}
}
private void relaceFrgment(int type) {
if (type % 2 == 0) {
relaceFrgment(new RightFragment());
} else {
relaceFrgment(new AnotherRightFragment());
}
}
private void relaceFrgment(Fragment fragment) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction tr = fm.beginTransaction();
tr.replace(R.id.fr_container, fragment);
// tr.commitAllowingStateLoss();
tr.commit();
}
I/xx: RightFragment The construction method is gone.
I/xx: RightFragment onAttach Gone...
I/xx: RightFragment onCreate Gone...
I/xx: RightFragment onCreateView Gone...
I/xx: RightFragment onActivityCreated...
I/xx: RightFragment onStart...
I/xx: RightFragment onResume...
I/xx: AnotherRightFragment The construction method is gone.
I/xx: AnotherRightFragment onAttach Gone...
I/xx: AnotherRightFragment onCreate Gone...
I/xx: RightFragment onPause...
I/xx: RightFragment onStop...
I/xx: RightFragment onDestroyView...
I/xx: RightFragment onDestroy...
I/xx: RightFragment onDetach...
I/xx: AnotherRightFragment onCreateView Gone...
I/xx: AnotherRightFragment onActivityCreated...
I/xx: AnotherRightFragment onStart...
I/xx: AnotherRightFragment onResume...
PS: Frgament replacing Frgament is somewhat different from Activity opening another activity to execute
4. When Activity exits, the life cycle of fragment s attached to Activity
I/xx: AnotherRightFragment onPause...
I/xx: MainActivity onPause
I/xx: AnotherRightFragment onStop...
I/xx: MainActivity onStop
I/xx: AnotherRightFragment onDestroyView...
I/xx: AnotherRightFragment onDestroy...
I/xx: AnotherRightFragment onDetach...
I/xx: MainActivity onDestroy
5. When a transaction joins addToBackStack(null);
private void relaceFrgment(Fragment fragment) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction tr = fm.beginTransaction();
tr.replace(R.id.fr_container, fragment);
tr.addToBackStack(null);//Click to return to the last Fragment
tr.commit();
}
I/xx: RightFragment The construction method is gone.
I/xx: RightFragment onAttach Gone...
I/xx: RightFragment onCreate Gone...
I/xx: RightFragment onCreateView Gone...
I/xx: RightFragment onActivityCreated...
I/xx: RightFragment onStart...
I/xx: RightFragment onResume...
The replacement operation is performed here.————————–
I/xx: AnotherRightFragment The construction method is gone.
I/xx: AnotherRightFragment onAttach Gone...
I/xx: AnotherRightFragment onCreate Gone...
I/xx: RightFragment onPause...
I/xx: RightFragment onStop...
I/xx: RightFragment onDestroyView...
I/xx: AnotherRightFragment onCreateView Gone...
I/xx: AnotherRightFragment onActivityCreated...
I/xx: AnotherRightFragment onStart...
I/xx: AnotherRightFragment onResume...
Summary: It can be seen from this that after adding ToBackStack (null) to something, if the addToBackStack() method is added to replace it, then RightFragment will not follow the onDestroy() method and onDetach() method ().
After clicking the return key
I/xx: AnotherRightFragment onCreateView Gone...
I/xx: AnotherRightFragment onActivityCreated...
I/xx: AnotherRightFragment onStart...
I/xx: AnotherRightFragment onResume...
The return key operation is performed here.————————–
I/xx: AnotherRightFragment onPause...
I/xx: AnotherRightFragment onStop...
I/xx: AnotherRightFragment onDestroyView...
I/xx: AnotherRightFragment onDestroy...
I/xx: AnotherRightFragment onDetach...
I/xx: RightFragment onCreateView Gone...
I/xx: RightFragment onActivityCreated...
I/xx: RightFragment onStart...
I/xx: RightFragment onResume...
As you can see, the return key is to destroy the current Fragment, so it will go, but the previous Fragment does not need to execute the onAttach() method and the onCreate() method because the addToBackStack() method (because it does not execute the onDestroy() method and the onDetach() method when it is replaced).
There are also some very good articles here:
1.Communication before Activity and Fragment - CSDN Blog
2.Life Cycle of Fragment - MeloDev's Blog - CSDN Blog The source framework here is very good: itsMelo/BuzzerBeater: Take the first step towards open source, learn from your first thoughts, and time proves it. The lazy loading implemented here is not very correct. Look at the following [1] [2] [3] [4] for your enlightenment.
[1] Use of Android fragment on Hidden Changed - CSDN blog This is for explaining the second blog post
[2] Fragment's setUserVisibleHint method implements lazy loading, but setUserVisibleHint doesn't work? - Leevey L - Blog Garden This is for explaining the second blog post
[3] TabLayout Use Details - Brief Books This is for explaining the second blog post
[4] Fragment's Life Cycle in ViewPagerz under Various Conditions - Material Poverty - CSDN Blog
3.Android - Basic Usage, Life Cycle and Details of Fragment - Brief Book
Android Face Sutra
1.Details of Android Development - Leakage Checking and Defect Filling (2): Easy to Forget and Understand - Brief Book
2.Java Interview Related (1) - Java Class Loading Process - Brief Book
If it's helpful to you, you can click on "Recommendation" oh (**)'