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

前端 未结 8 2051
轮回少年
轮回少年 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 07:10

    I like this question and the answers to it, but so far there isn't coverage of less frequently used callbacks like onPostCreate() or onPostResume(). Steve Pomeroy has attempted a diagram including these and how they relate to Android's Fragment life cycle, at https://github.com/xxv/android-lifecycle. I revised Steve's large diagram to include only the Activity portion and formatted it for letter size one-page printout. I've posted it as a text PDF at https://github.com/code-read/android-lifecycle/blob/master/AndroidActivityLifecycle1.pdf and below is its image:

    0 讨论(0)
  • 2020-11-21 07:12

    ANDROID LIFE-CYCLE

    There are seven methods that manage the life cycle of an Android application:

    • onCreate()
    • onStart()
    • onResume()
    • onRestart()
    • onPause()
    • onStop()
    • onDestroy()

    Answer for what are all these methods for:

    Let us take a simple scenario where knowing in what order these methods are called will help us give a clarity why they are used.

    • Suppose you are using a calculator app. Three methods are called in succession to start the app.

    onCreate() - - - > onStart() - - - > onResume()

    • When I am using the calculator app, suddenly a call comes the. The calculator activity goes to the background and another activity say. Dealing with the call comes to the foreground, and now two methods are called in succession.

    onPause() - - - > onStop()

    • Now say I finish the conversation on the phone, the calculator activity comes to foreground from the background, so three methods are called in succession.

    onRestart() - - - > onStart() - - - > onResume()

    • Finally, say I have finished all the tasks in calculator app, and I want to exit the app. Futher two methods are called in succession.

    onStop() - - - > onDestroy()


    There are four states an activity can possibly exist:

    • Starting State
    • Running State
    • Paused State
    • Stopped state

    Starting state involves:

    Creating a new Linux process, allocating new memory for the new UI objects, and setting up the whole screen. So most of the work is involved here.

    Running state involves:

    It is the activity (state) that is currently on the screen. This state alone handles things such as typing on the screen, and touching & clicking buttons.

    Paused state involves:

    When an activity is not in the foreground and instead it is in the background, then the activity is said to be in paused state.

    Stopped state involves:

    A stopped activity can only be bought into foreground by restarting it and also it can be destroyed at any point in time.

    The activity manager handles all these states in such a way that the user experience and performance is always at its best even in scenarios where the new activity is added to the existing activities

    0 讨论(0)
提交回复
热议问题