Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Matthew Francis
Matthew Francis
6,967 Points

Calling super.methodName() on an Activity Life Cycle?

public class MainActivity extends AppCompatActivity {

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

If my understanding is correct, when you create super.Oncreate(), you actually are calling the pre-defined onCreate() method from AppCompatActivity.class (MainActivity extends AppCompatactivity, so AppCompatActivity is the super).

What I'm confused about is with onResume(). I saw a code like this:

@Override
protected void onResume(){
  super.onResume();
}

In the AppCompatActivity docs, there are no OnResume() method, there are only onPostResume(). So how is it calling super.onResume()?

https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

If you look at the class hierarchy near the top of the docs, you'll see that AppCompatActivity is a decedent of Activity. Even if AppCompatActivity doesn't have it's own onResume() there is still one in the parent classes to call/override.

Mahmoud Amin
Mahmoud Amin
Courses Plus Student 6,269 Points

As i understand now that we call super to bring a method from a superclass Is that true!!!!!