Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - What is wrong with onStart method?

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?

like image 376
Joe Rakhimov Avatar asked Oct 05 '15 09:10

Joe Rakhimov


People also ask

Should I use onStart or onResume?

onResume() -> called when the activity is in the foreground, or the user can interact with the Activity. Show activity on this post. onStart() means Activity entered into visible state and layout is created but can't interact with this activity layout. Resume() means now you can do interaction with activity layout.

How do I use onStart on Android?

onStart(): This method is called when an activity becomes visible to the user and is called after onCreate. onResume(): It is called just before the user starts interacting with the application. onPause(): It is called when the app is partially visible to the user on the mobile screen.

What happens android onStart?

The onStart() call makes the activity visible to the user, as the app prepares for the activity to enter the foreground and become interactive. For example, this method is where the app initializes the code that maintains the UI.

What is the difference between onCreate () and onStart ()?

onCreate() is called when the when the activity is first created. onStart() is called when the activity is becoming visible to the user.


1 Answers

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).

like image 119
denis_lor Avatar answered Nov 14 '22 04:11

denis_lor