Android activity life cycle - what are all these methods for?

前端 未结 8 2031
轮回少年
轮回少年 2020-11-21 06:18

What is the life cycle of an Android activity? Why are so many similar sounding methods (onCreate(), onStart(), onResume()) called dur

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 06:55

    I run some logs as per answers above and here is the output:

    Starting Activity

    On Activity Load (First Time)
    ————————————————————————————————————————————————
    D/IndividualChatActivity: onCreate: 
    D/IndividualChatActivity: onStart: 
    D/IndividualChatActivity: onResume: 
    D/IndividualChatActivity: onPostResume: 
    
    Reload After BackPressed
    ————————————————————————————————————————————————
    D/IndividualChatActivity: onCreate: 
    D/IndividualChatActivity: onStart: 
    D/IndividualChatActivity: onResume: 
    D/IndividualChatActivity: onPostResume: 
    
    OnMaximize(Circle Button)
    ————————————————————————————————————————————————
    D/IndividualChatActivity: onRestart: 
    D/IndividualChatActivity: onStart: 
    D/IndividualChatActivity: onResume: 
    D/IndividualChatActivity: onPostResume: 
    
    OnMaximize(Square Button)
    ————————————————————————————————————————————————
    D/IndividualChatActivity: onRestart: 
    D/IndividualChatActivity: onStart: 
    D/IndividualChatActivity: onResume: 
    D/IndividualChatActivity: onPostResume: 
    

    Stopping The Activity

    On BackPressed
    ————————————————————————————————————————————————
    D/IndividualChatActivity: onPause:
    D/IndividualChatActivity: onStop: 
    D/IndividualChatActivity: onDestroy: 
    
    OnMinimize (Circle Button)
    ————————————————————————————————————————————————
    D/IndividualChatActivity: onPause: 
    D/IndividualChatActivity: onStop: 
    
    OnMinimize (Square Button)
    ————————————————————————————————————————————————
    D/IndividualChatActivity: onPause: 
    D/IndividualChatActivity: onStop: 
    
    Going To Another Activity
    ————————————————————————————————————————————————
    D/IndividualChatActivity: onPause:
    D/IndividualChatActivity: onStop: 
    
    Close The App
    ————————————————————————————————————————————————
    D/IndividualChatActivity: onDestroy: 
    

    In my personal opinion only two are required onStart and onStop.

    onResume seems to be in every instance of getting back, and onPause in every instance of leaving (except for closing the app).

提交回复
热议问题