Extending class for activity

≯℡__Kan透↙ 提交于 2019-12-03 02:54:50

What they mean is the following:

Normally you would have:

public class MyActivity extends Activity{...}

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:

public class BaseActivity extends Activity{

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //My menu code  
    }
}

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

public class MyActivity extends BaseActivity{...}

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.

It's quite simple really.

MainMenuActivity

public class MainMenuActivity extends Activity {
   //Override or add whatever functionality you want other classes to inherit.
}

MainActivity

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

SubActivity

public class SubActivity extends MainMenuActivity {
   //Add what is specific to SubActivity. The menu will be inherited from MainMenuActivity.
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!