8

I am trying to understand Android Activity lifecycle. For that purpose, I have created Activity where I have overridden all lifecycle methods(onCreate, onStart, onRestart, onResume, onPause, onStop, onDestroy):

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Log.d("ActivityTutorial", "onCreate");
    }

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

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

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

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

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

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

}

I put breakpoints on the lines where I am logging using Log.d(...). Then I tried to debug. onCreate method is okay - it is calling when activity is created.

Strange situation is starting onStart method. According to Android documentation: "onStart() Called when the activity is becoming visible to the user." But when I debug, it comes to onStart method but Button which is on this Activity, not visible yet.

enter image description here

I thought it supposed to be seen onResume() - after calling onStart() method. But button was not visible.

enter image description here

Only after onResume method, button was visible.

enter image description here

So my question is what is wrong with onStart and onResume methods? Maybe I am doing something not in the way it supposed to be done?

Joe Rakhimov
  • 4,713
  • 9
  • 51
  • 109
  • 1
    since you are holding break point at onStart() and onResume() so main thread not able to update UI because it is waiting for break point to be passed. That's why you are seeing this behaviour. – Geek Mar 17 '17 at 06:25

4 Answers4

2

No. The onResume() method makes visible of Activity. As you said thanks to the Documentation: "onStart() Called when the activity is becoming visible to the user".

And if you read carefully: "onResume() Called when activity will start interacting with the user."

UPDATE:

Keep in mind that onResume is not the best indicator that your activity is visible to the user; a system window such as the keyguard may be in front. Use onWindowFocusChanged(boolean) to know for certain that your activity is visible to the user (for example, to resume a game).

denis_lor
  • 6,212
  • 4
  • 31
  • 55
  • According to your answer, onResume method makes Activity visible & intractable. So in that case what is purpose of onStart() method? – Joe Rakhimov Oct 05 '15 at 09:38
  • No method is guaranteed to make your layout visible, see Documentation saying: "Keep in mind that onResume is not the best indicator that your activity is visible to the user; a system window such as the keyguard may be in front. Use onWindowFocusChanged(boolean) to know for certain that your activity is visible to the user (for example, to resume a game). " – denis_lor Oct 05 '15 at 09:43
  • Now I know how to catch when UI visible to user - using onWindowFocusChanged method. In that case what is purpose of onStart and onResume method? – Joe Rakhimov Oct 05 '15 at 10:00
  • 1
    @JoeRichard they act as places where you should put certain code. E.g. in onStart you can create a service, and in onResume you can start/restart the service and in onPause you can stop the service. Just an example. – Tim Oct 05 '15 at 10:22
  • 1
    As far as I can go with this explanation, there is no better way I know how to explain this, hope this link will clarify your thoughts about the purpose of the Android App Lifecycle convention names: http://stackoverflow.com/questions/4553605/difference-between-onstart-and-onresume . – denis_lor Oct 05 '15 at 10:23
  • 1
    @denis_lor, thank you for patience and valuable time. Now I know why we need lifecycle methods and can explain to my students – Joe Rakhimov Oct 05 '15 at 10:34
  • @JoeRichard You are welcome! If my answer helped you in some way, checked it as correct answer :) this will help me build online reputation here in stackoverflow :) thank you too! – denis_lor Oct 05 '15 at 11:04
2

firstly, when the first enter the activity, the view is not added to the window at onStart and onResume. the addView to the window called after the onResume method. you can see the ActivityThread handleResumeActivity method.

public void handleResumeActivity(IBinder token, boolean finalStateRequest, boolean isForward,String reason) {
// it will call activity onResume in the end
        final ActivityClientRecord r = performResumeActivity(token, finalStateRequest, reason);
...
if (r.activity.mVisibleFromClient) {
                r.activity.makeVisible();
            }
...
}
// this is the activity method,it will add view to window, and show
 void makeVisible() {
        if (!mWindowAdded) {
            ViewManager wm = getWindowManager();
            wm.addView(mDecor, getWindow().getAttributes());
            mWindowAdded = true;
        }
        mDecor.setVisibility(View.VISIBLE);
    }

so as the document said, the onResume is not the best indicator

Keep in mind that onResume is not the best indicator that your activity is visible to the user; a system window such as the keyguard may be in front. Use onWindowFocusChanged(boolean) to know for certain that your activity is visible to the user (for example, to resume a game).

so if you want to confirm the activity is visible, please use the onWindowFocusChanged.

after that, we press the Home key, then open the activity again, this time the onStart method can see the UI is visible.

that is the real reason.

Lenoarod
  • 3,441
  • 14
  • 25
0

well activity will be visible on onResume() method not on onStart(). see the android documentation and activity lifecycle.

Nikhil bohra
  • 114
  • 1
  • 5
0

The main reason of onStart() method to be in Activity Lifecycle is to ready the main UI thread for the user. It grasps the values assigned in onCreate() methods for UI, and ready activity UI to become visible to the user.

The second reason is that we don't have to create an activity every time when onStop() method calls. It's like a checkpoint in activity lifecycle to go back to previous states without doing extra loading.

Now the onStart() methods done loading very quickly and start onResume() method. The onResume() allows the user's interaction with the UI. But as you say that your UI comes little after the loading of onResune() method is because the inaccessible UI is of no use in onStart() method. And if a user tries to access the inaccessible UI but can't perform anything, that displays a bad impression. So UI will only become visible when it's accessible and ready for user interaction.

Ashutosh Sagar
  • 981
  • 8
  • 20