Activity life cycle

Activity life cycle. After the Activity is deployed to the Android application, it will switch between different states after running with the application, and will call back different methods while switching the state. This state is called the life cycle.

Activities have four states: running state, paused state, stopped state, destroyed state


onCreate: Called when the activity is created for the first time. In this method, initialization operations, such as loading layout binding events, should be completed.

onStart: Called when the activity changes from invisible to visible

onResume: Called when the activity is ready to interact with the user, at this time the activity must be at the top of the return stack

onPause: Called when the system is ready to start or resume another activity. In this method, the consumed CPU resources are released. The execution speed of this method must be fast, otherwise it will affect the use of the new stack top activity.

onStop: called when the activity is completely invisible, the difference from onPause: if the new activity started is a dialog-like activity, onPause will be called, but onStop will not

onDestory: called before the activity is destroyed, after which the activity becomes destroyed

onRestart: called when the activity is restarted

public class LifeCycleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_life_cycle);
        Log.d("lifeCycle","---onCreate---"); //output in the log
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("lifeCycle","---onStart---");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("lifeCycle","---onResume---");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("lifeCycle","---onRause---");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("lifeCycle","---onStop---");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("lifeCycle","---onRestart---");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy ();
        Log.d("lifeCycle","---onDestory---");
    }
}

Displayed in Logcat when the Activity is opened

04-28 08:42:40.974 24084-24084/com.wangkangli.activitystudy D/lifeCycle: ---onCreate---
04-28 08:42:40.978 24084-24084/com.wangkangli.activitystudy D/lifeCycle: ---onStart---

04-28 08:42:40.979 24084-24084/com.wangkangli.activitystudy D/lifeCycle: ---onResume---

Indicates that only onCreate, onStart, and onResume are called.

Displayed in Logcat when the return key is clicked

04-28 09:01:12.895 24084-24084/com.wangkangli.activitystudy D/lifeCycle: ---onRause---
04-28 09:01:13.243 24084-24084/com.wangkangli.activitystudy D/lifeCycle: ---onStop---
    ---onDestory---

onResume: will be called when leaving the app and returning

Logcat shows when you click the home button to go back to the background

04-28 09:15:28.360 24084-24084/com.wangkangli.activitystudy D/lifeCycle: ---onRause---

04-28 09:15:28.391 24084-24084/com.wangkangli.activitystudy D/lifeCycle: ---onStop---

What if the event is recycled?

There is an activity A in the application. The user starts activity B on the basis of activity A, and A enters the stopped state. At this time, the system memory is sufficient to recycle activity A. When the user presses Back to return to activity A, onRestart will not be executed. , instead onCreate is executed because the activity will be recreated. But when you open it again, the temporary data at that time will be a trivial matter, which will seriously affect the user experience!

Activity provides an onSaveInstanceState() callback method, which will be called before the activity is recycled, so we use this method to solve this problem

onSaveInstanceState() carries a Bundle type parameter, which provides a series of methods to save data, such as putString() to save strings, etc.

@0verride
protected void onSaveInstanceState(Bundle outState) {  
	super. onSaveInstanceState (outState) ;
	String tempData = “Something you just typed”;
	outState.putString ("data_key”,tempData);
}
From this, data is saved, and the onCreate method is rewritten
@0verride
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);
	}
//…
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325723027&siteId=291194637