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 Build a Simple Android App (retired 2014) Shaking Things Up The Activity Lifecycle

Shane Desouza
Shane Desouza
3,523 Points

What parameter are you supposed to pass in

Hi,

I'm having trouble with this code challenge as I can't figure out what parameter to pass in and how to execute the parent class of the MainActivity class. Can someone please help me?

Thanks!

2 Answers

Joe Brown
Joe Brown
21,465 Points

You have to call the parent or "super" classes version of onCreate(). The MainActivity class extends Activity class, which means that MainActivity has access to all of the methods that the Activity class has. Activity class is the parent class, or super class, of MainActivity. So when you override onCreate, onPause, etc, you have to "call up" to the super classes version of that same method. For onCreate you want to pass in any data that you want to be saved for when onCreate is called again, usually you want to "save the state"..

The code challenge does not require you to override the onCreate( ) method. However the code challenge is incorrect in doing so and does not conform to the Java coding standards and really just promotes a bad coding habit.

Anytime you have a class that "extends" another class, such as the Activity class, and you're taking one of the parent class' methods and adding your own code to it such as the onCreate( ) method then you can/should pass a flag to the compiler let it know that the method invoked in from the sub or child class of the parent class that it should instead use the onCreate method from the child class instead of the parent class. This is just an optimization factor but it also enforces error checking by requiring that the method overridden is actually declared in the parent class and if it is not then the code will not compile.
The way you do this is to include "@Override" before the method's signature in the child class' implementation of the method.
However there is still code that you need for that is included inside of the method's body within the parent class regardless of the code you "added" to it. You obviously don't want to have to write all the code from the onCreate( ) method in every single class that is derived from the Activity class so instead you use a default reference variable that is included in any class that extends another class named "super".
To include the original code in your new version of a method the very first line of code you have (it has to be the first line) super invoke the same method signature that its' enclosed within and pass in the parameters.

Here is an example in the code shown below:

@Override
protected void onCreate( Bundle savedInstanceState )
{

    super.onCreate( savedInstanceState );

    setContentView( R.layout.mylayout ); 

}