Skip to content Skip to sidebar Skip to footer

Extending Class For Activity

I'm totally new to Android (Java) Development and I'm so excited about it! The developers guide of Google is fantastic and I learned a lot in a small time. It even keeps me awake d

Solution 1:

What they mean is the following:

Normally you would have:

publicclassMyActivityextendsActivity{...}

If you have 4-5-6... of those activities, and each of them uses the same menu code, you could just copy and paste the code 4-5-6.. times. Or you could do this:

publicclassBaseActivityextendsActivity{

    @OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
        //My menu code  
    }
}

And use this class for your 4-5-6... Activities:

publicclassMyActivityextendsBaseActivity{...}

This way you don't need to copy your menu creation code into all your Activities, and moreover, you don't have to edit 4-5-6... classes to edit a small bit of creation of the menu. The menu code is now also in MyActivity.


You could also have a look here, it explains what extends means.

Solution 2:

It's quite simple really.

MainMenuActivity

publicclassMainMenuActivityextendsActivity {
   //Override or add whatever functionality you want other classes to inherit.
}

MainActivity

publicclassMainActivityextendsMainMenuActivity {
   //Add what is specific to MainActivity. The menu will be inherited from MainMenuActivity.
}

SubActivity

publicclassSubActivityextendsMainMenuActivity {
   //Add what is specific to SubActivity. The menu will be inherited from MainMenuActivity.
}

Post a Comment for "Extending Class For Activity"