Rockers23
Rockers23

Reputation: 803

Android Navigation Drawer Activity

I'm using Navigation Drawer Activity in my app and I have a little problem. When I select item on navigation items the menu icon change to arrow like the image below: enter image description here

And what I want is this :

enter image description here

Second thing , When navigate from main fragment to child fragment , how can I enable back function when I click arrow icon?

This is my code :

@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);
    displayView();

 public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    Fragment fragment = null;
    int id = item.getItemId();
    String title ="";
    boolean isOK = true;
    if (id == R.id.nav_home) {
        // Handle the camera action
         fragment = new HomeFragment();
        title = getString(R.string.nav_item_home);
    } else if (id == R.id.nav_store) {
        if (MyHelper.licationServiceIsActive(MainActivity.this)){
        fragment = new MagasinsFragment();
        title = getString(R.string.nav_item_store);}else {
            isOK = false;
        }

    } else if (id == R.id.nav_catalog) {
        fragment = new AllCatalogueFragment();
        title = getString(R.string.nav_item_catalog);

    } else if (id == R.id.nav_parking) {
        if (MyHelper.licationServiceIsActive(MainActivity.this)){
        fragment = new ParkingFragment();
        title = getString(R.string.nav_item_parking);}else {
            isOK = false;
        }

    } else if (id == R.id.nav_cart) {
        fragment = new AllListFragment();
        title = getString(R.string.nav_item_cart);
        //fragment = new ListeFragment();
    } else if (id == R.id.nav_settings) {
        fragment = new SettingsFragment();
        title = getString(R.string.action_settings);
    }
    if (isOK) {
if (fragment != null) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    int count = fragmentManager.getBackStackEntryCount();
    for (int i = 0; i < count; ++i) {
        fragmentManager.popBackStack();
    }
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.container_body, fragment);


    fragmentTransaction.commit();

    // set the toolbar title
    getSupportActionBar().setTitle(title);
}
  }else {
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("Information");
dialog.setMessage("Le service de localisation est désactivé.\nVoulez-vous l'activer?");
dialog.setPositiveButton("Activer", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
        // TODO Auto-generated method stub
        Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(myIntent);
        //get gps
    }
});
dialog.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
        // TODO Auto-generated method stub

    }
});
dialog.show();
   }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);

    return true;
}

Upvotes: 0

Views: 332

Answers (3)

Akshay Chordiya
Akshay Chordiya

Reputation: 4841

I think you should disable the up icon of the ActionBar or Toolbar

For ActionBar:

getActionBar().setDisplayHomeAsUpEnabled(false);

Upvotes: 2

Priya Jagtap
Priya Jagtap

Reputation: 1023

To navigate through BAck Button try this, On the Onclick of back button call OnBackPressed(); method to get back to previous activity.

Upvotes: 0

Satish Silveri
Satish Silveri

Reputation: 393

For the first question: You might be using ActionBarDrawerToggle method due to which it changes the menu button to back button!! Simply remove that code if you are using it.

For Second Question:

To implement Back functionality in Fragments you just need to PopBackStack()

for example:

   public void moveBack()
    {
//FM =FragmentManager object
        if(FM!=null && FM.getBackStackEntryCount()>0)
       {
         FM.popBackStack() //to pop fragment from backstack
       }
       else
       {
         finish();  //for activity exit
       }
    }

make sure you add fragment to BackStack.

Upvotes: 0

Related Questions