Amit Pal
Amit Pal

Reputation: 11052

Don't have OnStart() in Android Activity

I am beginner in Android and was reading official Android tutorial on Activity life cycle which state that OnResume() can only be called after OnStart(). Is that true?

If yes, then there is a class (MainActivity) extends by Android.Activity which doesn't have OnStart() and OnStop()methods. I am listing down all methods in this class file:

OnCreate()
OnResume()
onPause()

launchNewActivity(){
startActivity(new Intent(this, SecondActivity.class));
finish();
}

I have following doubt here:

It look like a stupid question but I am confused here :)

Upvotes: 1

Views: 2105

Answers (3)

codePG
codePG

Reputation: 1744

You seem to have confusion with the Activity Life Cycle.

OnResume() can only be called after OnStart() ?

One point is that you don't invoke (call) these methods. The are automatically called when an activity is created. And also there are some methods called automatically when the activity is paused.

What you can do is that, override such methods to do some action when these methods gets executed automatically. Like knowing the sequence of the execution of these methods, you can do an action at any stage.

And to answer your question, YES onResume() is called after the onStart() method by default. That is the sequence.

There is a class (MainActivity) extends by Android.Activity which doesn't have OnStart() and OnStop()methods?

Some of these methods are not automatically implemented. And you can implement all the life cycle methods in any activity. For example if you wont to perform any action when the onStop() method is called you can simply override that method in the activity you wont it, like the example below.

//Overriding the onStop() method to do actions of your interest
@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    Log.d("TAG", "On Stop");
    //Do the action you would like to perform
}

If there is no onStart() method then how can activity will get start?

If there is no method, still that method is being called. In a case you need to perform specific actions there, then you override the method in you activity class and specify the actions to be performed when that method is being automatically called when the activity gets created.

I can find onStart() and onStop() method in SecondActivity. Is that why I don't have these method in MainActivity?

You can have all the life cycle methods in any of you activity, including MainActivity. Use them when you want to override the methods. If you don't have any special action to be performed in that sequence, just leave them and you activity works fine as the default will be automatically invoked.

The below image shows the sequence of how the life cycle methods are being called. enter image description here

.

I would like to recommend you to watch these short videos which excellently explains how the Activity Life Cycle works.

http://www.youtube.com/watch?v=Qs-lGmaMIDk&index=24&list=PLlxmoA0rQ-LyCGSSD_nuPAmXDSR_FU0RR

http://www.youtube.com/watch?v=F5XP1bYBRdI&list=PLlxmoA0rQ-LyCGSSD_nuPAmXDSR_FU0RR&index=25

I hope this helps you!

Upvotes: 5

dmananes
dmananes

Reputation: 104

Yes, onResume() can only be called after onStart().

When you extends MainActivity from Activity, it means that you will get all the Activity methods in MainActivity. That's inheritance. Check it out here

If you want to change any of those methods, then you have to override it:

@Override
protected void onResume() {
    super.onResume();
    Log.i(TAG, "On Resume...");
}

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007296

OnResume() can only be called after OnStart(). Is that true?

onStart() will have been called by the time onResume() is called.

If there is no onStart() method then how can activity will get start?

You are inheriting onStart() from Activity.

Upvotes: 2

Related Questions