-1

How to use the Android default Navigation in other Activities?

I don't want to use the back button.

MainActivity:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

TextView tvhead,tv6,tv7,tv8,tv9,tvbottom;
Typeface schriftart_courier_prime;

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


    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);

    FragmentManager fm = getFragmentManager();
    fm.beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();
}

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_about) {

        Intent getNameScreenIntent2 = new Intent (this, AboutActivity.class);


        final int result = 1;

        startActivity(getNameScreenIntent2);

        return true;

        //Actionbar element für später
    }else if (id == R.id.action_phone){

    }

    return super.onOptionsItemSelected(item);
}


@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {

    FragmentManager fm = getFragmentManager();

    int id = item.getItemId();

    if (id == R.id.nav_home) {
        fm.beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();
    } else if (id == R.id.nav_experiments) {

        Intent getNameScreenIntent = new Intent(this, ExperimentsList.class);

        final int result = 1;

        startActivity(getNameScreenIntent);
    } else if (id == R.id.nav_website) {

        Intent getNameScreenIntent = new Intent(this, Web.class);

        final int result = 1;

        startActivity(getNameScreenIntent);
    } else if (id == R.id.nav_share) {

        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("text/plain");
        share.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_text));
        startActivity(Intent.createChooser(share, getString(R.string.share_title)));
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Can you please make it easier for people to help you by either reducing your sample code to the minimum needed to demonstrate the problem, or giving more explanation, or both? – Graham Asher Dec 13 '15 at 13:35

1 Answers1

0

Have all the activities that need that drawer inherit from a custom abstract Activity class, that adds the drawer in its onCreate method.

make a class something like:

public abstract class MyNavigationActivity extends AppCompatActivity { 
   @Override 
   protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
     setSupportActionBar(toolbar);
     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);
   } 
} 

then:

public class MyActivity extends MyNavigationActivity{ 
   @Override 
   protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState);
       FragmentManager fm = getFragmentManager();
       fm.beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();
   }
} 
denvercoder9
  • 2,979
  • 3
  • 28
  • 41
yedidyak
  • 1,964
  • 13
  • 26