0

The project has the activity of the toolbar. Activity in dynamically changing fragments. Depending on the fragment ought to change content toolbar. Turning to the second fragment ought to appear in the toolbar nazat arrow which returns to the previous fragment.

public class StartPageActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private Toolbar mToolbar;
private NavigationView mNavigationView;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle drawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start_page_activity);
    setTitle("ForgetFul");
    getFragment(new MainFragment());
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    mNavigationView = (NavigationView) findViewById(R.id.main_drawer);
    mNavigationView.setNavigationItemSelectedListener(this);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_widget);
    drawerToggle
            = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close);
    mDrawerLayout.setDrawerListener(drawerToggle);
    drawerToggle.syncState();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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_settings) {
        Log.i("Activity", "Setting");
        return true;
    }
    if (android.R.id.home == id) {
        Log.i("One", "Dude");
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}...

In the active from the start displayed MainFragment. In the second activity, I use the following code:

((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

"Up" Arrow appeared, but when I click on arrow, all time open the NavigationView. how to fix it? P.S. Sorry for English :(

Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51
one_man
  • 135
  • 2
  • 14
  • 1
    Are you able to return to the previous fragment on pressing the back key (of phone)? – Aditya Naique Sep 14 '15 at 12:17
  • I'm specifically asking about the back key, NOT the Up arrow in your toolbar. – Aditya Naique Sep 14 '15 at 12:18
  • yap.... when press back key (phone) i return to the previous fragment – one_man Sep 14 '15 at 12:32
  • onBackPressed () when menuID == android.R.id.home ? – Ramesh Sep 14 '15 at 12:34
  • when i delete its mNavigationView = (NavigationView) findViewById(R.id.main_drawer); mNavigationView.setNavigationItemSelectedListener(this); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_widget); drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close); mDrawerLayout.setDrawerListener(drawerToggle); drawerToggle.syncState(); From activity. menuId == android.R.id.home worked, but i cant delete this part of code. – one_man Sep 14 '15 at 12:36

1 Answers1

0

Try this.
First, remove implements NavigationView.OnNavigationItemSelectedListener , and also comment out the overidden methods and variables.

Then overide the Activity's onBackPressed() method like this:

    @Override
public void onBackPressed() {
    FragmentManager fragmentManager = getFragmentManager();
    if (fragmentManager.getBackStackEntryCount() != 0) {
        fragmentManager.popBackStack();
    } else {
        super.onBackPressed();
    }
}

This will make sure that the previous fragments are getting displayed on pressing the Back key.

Then, simply call it in onOptionsItemSelected() method:

    if (id == android.R.id.home) {
        onBackPressed();
        return true;
    }
Aditya Naique
  • 1,120
  • 13
  • 25
  • I implements this... and i have onBackpressed; but if (id == android.R.id.home) { onBackPressed(); return true; } dont work – one_man Sep 14 '15 at 12:39
  • its work when i delete part of code...... mNavigationView = (NavigationView) findViewById(R.id.main_drawer); mNavigationView.setNavigationItemSelectedListener(this); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_widget); drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close); mDrawerLayout.setDrawerListener(drawerToggle); drawerToggle.syncState(); – one_man Sep 14 '15 at 12:40
  • Make sure you've not set a custom Up icon. It should be only `final ActionBar ab = ((MainActivity) getActivity()).getSupportActionBar(); ab.setDisplayHomeAsUpEnabled(true);` If you're setting a custom Up icon then you have to reference that in `if (id == R.id.my_custom_up_icon)` – Aditya Naique Sep 14 '15 at 12:43
  • Refer the MainActivity and CheeseDetailActivity classes from Chris Banes' Cheesquare app, and hopefully you'll detect the bug. :) https://github.com/chrisbanes/cheesesquare/tree/master/app/src/main/java/com/support/android/designlibdemo – Aditya Naique Sep 14 '15 at 12:59