2
@Override
protected void onStop() {
super.onStop();  // Always call the superclass method first

// Save the note's current draft, because the activity is stopping
// and we want to be sure the current note progress isn't lost.
ContentValues values = new ContentValues();
values.put(NotePad.Notes.COLUMN_NAME_NOTE, getCurrentNoteText());
values.put(NotePad.Notes.COLUMN_NAME_TITLE, getCurrentNoteTitle());

getContentResolver().update(
        mUri,    // The URI for the note to update.
        values,  // The map of column names and new values to apply to them.
        null,    // No SELECT criteria are used.
        null     // No WHERE columns are used.
        );
}

This is a code of activity life cycle. I don't understand what is the purpose of using super.onStop() here ? and what does super class method mean ?

vhd
  • 2,008
  • 6
  • 20
  • 21

4 Answers4

0

If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super.

So by calling super.onStop() you are calling the onStop method of the baseclass (Activity).

When the user leaves your activity, the system calls onStop() to stop the activity. So it is essential to call the onStop method to stop the activity. But as you are overriding the onStop method to implement your own feature in it, if you dont call the onStop method of super class then the common task of onStop will not be done. That's why you have to call this

stinepike
  • 54,068
  • 14
  • 92
  • 112
0

The way the Android SDK works is quite frankly... ugly as hell. There may be some anti-patterns that don't exist in this SDK but I've yet to find one.

You have to subclass the various Activity/Fragment/Service etc classes to create an app then override the various methods to do anything useful and then are required to call the superclass implementation of the method on all the lifecycle methods both in addition to and prior to whatever it is you want to do.

TIP: don't think about it, just do it. Check the documentation (should it exist) and always call the super.onXXX first wherever it is specified.

Mark S
  • 51
  • 3
0

It is method of derived class, so it is must to call using super otherwise it will throw exception for details explanation read these links

Activity life cycle

Android Guide

JNI_OnLoad
  • 5,472
  • 4
  • 35
  • 60
0

As onStop() is android's Activity life cycle method' and super class provides implementation for onStop() method, android calls this method when an Activity going to stop, when you override onStop() method in your class, if you skip super.onStop() this will cause to not to run actual implementation of onStop method which is provided by android, and many house cleaning stuff which were should be performed will never be run, thats why android forces you to if you override onStop() you must call super.onStop(). Forcing to write super.onStop() seems anti pattern, there could be other ways too.

Yahya Arshad
  • 1,626
  • 2
  • 19
  • 34