简体   繁体   中英

Can I make a base class that I inherit this information from?

My boss recently suggested to me that I use inheritance with the classes in my app. There is one method that I use quite often, but the .this class is changed.

Here is the base code I use:

public class CURRENTCLASS extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_CURRENTCLASS);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(CURRENTCLASS.this, ActivityMenu.class);
            startActivity(intent);
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

}

@Override

public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.about) { //goes to Our company page
        Intent intent = new Intent(CURRENTCLASS.this, ActivityAbout.class);
        startActivity(intent);
    }
    else if (id == R.id.news) { // goes to News and events page
        Intent intent = new Intent(CURRENTCLASS.this, ActivityNewsEvents.class);
        startActivity(intent);
    }
    else if (id == R.id.serve) { // markets we serve
        Intent intent = new Intent(CURRENTCLASS.this, ActivityMarkets.class);
        startActivity(intent);
    }
    else if (id == R.id.seta) { //seta and aquisition support
        Intent intent = new Intent(CURRENTCLASS.this, ActivitySETA.class);
        startActivity(intent);
    }
    else if (id == R.id.software) { //software and systems dev
        Intent intent = new Intent(CURRENTCLASS.this, ActivitySoftware.class);
        startActivity(intent);
    }
    else if (id == R.id.training) { //intelligence analytics and training
        Intent intent = new Intent(CURRENTCLASS.this, ActivityAnalytics.class);
        startActivity(intent);
    }
    else if (id == R.id.QACSTUFF) { //QAC
        Intent intent = new Intent(CURRENTCLASS.this, ActivityQAC.class);
        startActivity(intent);
    }
    else if (id == R.id.careers) { //Careers
        Intent intent = new Intent(CURRENTCLASS.this, ActivityCareers.class);
        startActivity(intent);
    }
    else if (id == R.id.contactUs) { //Contact us
        Intent intent = new Intent(CURRENTCLASS.this, ActivityContact.class);
        startActivity(intent);
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
    }

}

I use this in the majority of my classes, I just change CURRENTCLASS to whatever class I copy it into. So, my question is, can I make this a class I inherit from so I can use these methods without copy/pasting them into every class? And if so, does anyone have resources/advice as to how I would do that?

Okay, clarification. I have 15+ classes in my android app that all have this same base code, except slightly changed. In the code, wherever it says "CURRENTCLASS" I will change that to say "ActivityMain" or "ActivityDevelopment" or whatever the activity is. I am wondering if there is a more efficient way to implement this, possibly by using inheritance or calling these methods in my other classes, without having to copy/paste all this code.

您可以创建BaseActivity (活动的父类)并在所有活动中扩展此类,然后使用swith case创建一个方法,并在所有onNavigationItemSelected方法中调用它

As it is an entire class you want to inherit from give Aj 27's answer consideration. You could place these method in another class and extend from it, therefore gaining access to all its methods without having to copy and paste loads of code all the time.

Read about inheritance here

Alternatively, if your activities are quite simple and not too different from one another in terms of functionality. You could consider fragments. You could have many fragments therefore using one MainActivity hosting your navigation bar.

Read about fragments here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM