Article Directory
One, the four states of the Activity life cycle
- Running state is
in a state where it is visible and can interact with users - Paused state
An Activity is covered by another transparent Activity or Dialog Activity, but it is still visible and loses the ability to interact with the user - Stop state
An Activity is completely covered by another transparent Activity or Dialog Activity, still maintaining all state and member information, but not visible - Disconnected
Activity was killed and recycled by the system or not started
Two, the seven methods of the Activity life cycle
- onCreate
- onStart
- onResume
- onPause
- onStop
- onDestroy
- onRestart
3. Examples of life cycle
The main code content is to rewrite seven methods (in the two activities are rewritten according to the following code seven methods)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show__image);
System.out.println("ShowImage_onCreate");
}
@Override
public void onStart()
{
super.onStart();
System.out.println("ShowImage_onStart");
}
public void onRestart()
{
super.onRestart();
System.out.println("ShowImage_onRestart");
}
public void onResume()
{
super.onResume();
System.out.println("ShowImage_onResume");
}
public void onPause()
{
super.onPause();
System.out.println("ShowImage_onPause");
}
public void onStop()
{
super.onStop();
System.out.println("ShowImage_onStop");
}
public void onDestroy()
{
super.onDestroy();
System.out.println("ShowImage_onDestroy");
}
1. Start another Activity in one Activity (complete coverage) and
jump to another Activity code
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btn_image;
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_image=findViewById(R.id.imageshow);
btn_image.setOnClickListener(this);
System.out.println("MainActivity_onCreate");
}
@Override
public void onClick(View view) {
Intent intent=new Intent();
intent.setAction("openimage");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
}
}
Run results
Open the app,
open another activity,
exit the new activity and APP
2. Start another activity in one activity (not completely covered)
, make the following modifications based on the above code.
Run the result.
Open the app,
open another activity, and
close the new activity and APP
3. Switch between horizontal and vertical screens
without modification
Open APP
horizontal screen to vertical screen
modify content
Run results After
adding changes, switch between horizontal and vertical screen Activity does not call any of the seven methods.
Fourth, the life cycle process of an Activity
Through the above example, the following process can be drawn. As a
whole, the change of Activity's life cycle is mainly the call of seven methods to realize the change of state.