android – Call super.onStart() in onResume-ThrowExceptions

Exception or error:

Firstly, We are know that it doesn’t make sense when we call super.onStart() in function onResume() callback. But for understanding about android activity life cycle. Can any one explain for me about it.
Let me introduction more about it. I create simple activity and try to run two code version also. In version 1, I override method onResume() and do nothing in this function.

Code:

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

And ofcourse, this code makes crash on Android when I try to run it.
However, when I run the second version, with a little change.

Code:

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

But now, the app can run correct and display my simple activity. App still can run when I replace super.onStart() with super.onCreate(null), super.onRestart(), super.onStop(), super.onDestroy().

This is my activity.

public class MainActivity extends Activity {
    private static String TAG = "LifeCycle " + MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

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

So, can anyone explain for me why this thing happen? Why the app still work when I do it?

How to solve:

Logically, its just method call of super. if you call onStart() from onResume(), technically it is correct but it is random call and Activity life cycle gets violated.

###

when you create a Activity you extend it’s parent class Activity. You can see the Parent Activity class by long press on your Mouse Scroller Button. There if you can see, you are actually sending the View from your subclass to the
Parent class , which will start your activity , yea i Know android will be calling the onStart method of your parent class not your sublcass. Thats a clear cut View for us. if you want to know how a activity lifecycle works it is simple. First when you start your app onCreate method is called and then onStart is called n then When your app is on foreground OnResume is called and then once your app is in background onPause is called , if you press back button onDestroy will be called. If you want to know about the complete lifecylce’s you can check it out at devlopers.android.com . Thankyou. I hope this was helpfull

Leave a Reply

Your email address will not be published. Required fields are marked *