Android development-the life cycle of Activity

One, the four states of the Activity life cycle

  1. Running state is
    in a state where it is visible and can interact with users
  2. 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
  3. Stop state
    An Activity is completely covered by another transparent Activity or Dialog Activity, still maintaining all state and member information, but not visible
  4. Disconnected
    Activity was killed and recycled by the system or not started

Two, the seven methods of the Activity life cycle

  1. onCreate
  2. onStart
  3. onResume
  4. onPause
  5. onStop
  6. onDestroy
  7. 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,
Insert picture description here
open another activity,
Insert picture description here
exit the new activity and APP
Insert picture description here
2. Start another activity in one activity (not completely covered)
, make the following modifications based on the above code.
Insert picture description here
Run the result.
Open the app,
Insert picture description here
open another activity, and
Insert picture description here
close the new activity and APP
Insert picture description here
3. Switch between horizontal and vertical screens
without modification
Open APP
Insert picture description here
horizontal screen to vertical screen
Insert picture description here
modify content
Insert picture description here
Run results After
Insert picture description here
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
Insert picture description here
whole, the change of Activity's life cycle is mainly the call of seven methods to realize the change of state.

Guess you like

Origin blog.csdn.net/qq_43279579/article/details/115248948