There's no reversal to the main pressure fragment on the Up-knock.



  • I've got activity. Toolbar and FrameLayout - fragments container. Navigation between fragments is carried out by means of Navigation Drawer from the Compatibility Library (v7).

    There's a button-gamburger on the Tulbar, which is pressurized by NavigationView - selecting a fragment - it's changing the main fragment.Fragment0) The button-gamburger is replaced by Up-knob (gun to left). I'd like to get back to the original fragment when we press it, and the side panel doesn't get out of the vault when there's no button-gamburger. But in the Up Knoopa case, the only thing that changed was his appearance, but not his behavior, is the side panel on it.

    This is my activity code:

    public class MainActivity extends AppCompatActivity {
    

    private String[] mNumberTitles;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private Toolbar toolbar;
    private ActionBarDrawerToggle mDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentById(R.id.fragment_container);
    if (fragment == null) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction
                .add( R.id.fragment_container, new Fragment0())
                .commit();
    }
    
    
    mNumberTitles = getResources().getStringArray(R.array.numbers_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    
    // Set the adapter for the list view
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mNumberTitles));
    // Set the list's click listener
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    
    initToolbar();
    
    
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            toolbar, R.string.drawer_open, R.string.drawer_close);
    
    // Set the drawer toggle as the DrawerListener
    mDrawerLayout.addDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState();
    
    
    // заменяем гамбургер на Up button во второстепенных фрагментах
    fragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
    
            } else {
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                mDrawerToggle.syncState();
            }
        }
    });
    

    }

    private void initToolbar() {
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    }

    /** Swaps fragments in the main content view */
    private void selectItem(int position) {

    Fragment fragment = null;
    switch (position) {
        case 0 : fragment = new Fragment0();
            break;
        case 1 : fragment = new Fragment1();
            break;
        case 2 : fragment = new Fragment2();
            break;
        default: fragment = new Fragment0();
            break;
    }
    
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, fragment)
            .addToBackStack(null)
            .commit();
    
    // Highlight the selected item, update the title, and close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mNumberTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
    

    }

    private void setTitle(String title) {
    if (toolbar != null)
    toolbar.setTitle(title);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case android.R.id.home: // it's for up button
            Log.d("!!!!!!!!!", "HOMe button is pressed");
            getSupportFragmentManager().popBackStack();
            return true;
        default:
            break;
    }
    return super.onOptionsItemSelected(item);
    

    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
    selectItem(position);
    }
    }

    Method onOptionsItemSelected(MenuItem item) (sighs)case android.R.id.home: We don't hit the top-knock.

    What am I doing wrong?



  • Try it. initToolbar Add:

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2