Nikola Božić
Nikola Božić

Reputation: 147

How to back from Fragment to primary Fragment with back button in Navigation Drawer Activity

I have Navigation Drawer Activity. I attached Fragment A(primary fragment). And in my Navigation Drawer menu i have other Fragments(B, C, D..). Now when i go to some Fragment from menu(B, C, D) and i press back button what happens is that app exit . What i want is when i press back button to back to Fragment A, than when i press back button again to exit app.

Here is my main activity:

 @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(toolbar);

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

   // FRAGMENT A starting fagment   
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    FragmentMainPage pocetnaFragment = new FragmentMainPage();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.relativeLayoutZaFragment, pocetnaFragment, "1");
    fragmentTransaction.commit();     

    getSupportActionBar().setTitle("Auto magazin");

    ActionBar.LayoutParams p = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    p.gravity = Gravity.LEFT;

    view = getLayoutInflater().inflate(R.layout.action_bar, null);

    ActionBar.LayoutParams params = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.WRAP_CONTENT,
            ActionBar.LayoutParams.MATCH_PARENT,
            Gravity.LEFT);

    TextView Title = (TextView) view.findViewById(R.id.actionbar_title);

    Title.setText("Auto magazin");

    getSupportActionBar().setCustomView(view,params);
    getSupportActionBar().setDisplayShowCustomEnabled(true); //show custom title
    getSupportActionBar().setDisplayShowTitleEnabled(false); //hide the default title
}

boolean doubleBackToExitPressedOnce = false;

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    assert drawer != null;


    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
        return;
    }
    else if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }

    this.doubleBackToExitPressedOnce = true;

    Toast.makeText(this, "Pritisnite još jednom za izlaz", Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            doubleBackToExitPressedOnce = false;
        }
    }, 2000);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {        
    getMenuInflater().inflate(R.menu.main, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    int id = item.getItemId();

    if (id == R.id.podesavanja) {

        startActivity(new Intent(this, Podesavanja.class));
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override

 // Other fragments, I want to back to Fragment A when click on back button
 // when i am in this Fragments
public boolean onNavigationItemSelected(MenuItem item)   {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.pocetna)   {

        FragmentMainPage fragmentMainPage = new FragmentMainPage() ;
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
               , fragmentMainPage, fragmentMainPage.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Auto magazin");
    }
    else if (id == R.id.info) {
         FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
         FragmentManager  manager = getSupportFragmentManager();
         manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Info");

        Bundle bundle = new Bundle();

        bundle.putString("href","http://www.magazinauto.com/category/info/");
        bundle.putString("title", "Info");
        fragmentMenuPages.setArguments(bundle);

    }
    else if (id == R.id.testovi)  {
        FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Testovi");

        Bundle bundle = new Bundle();
        bundle.putString("href", "http://www.magazinauto.com/category/testovi/");

        bundle.putString("title", "Testovi");
        fragmentMenuPages.setArguments(bundle);

    }
    else if (id == R.id.promocije)  {
        FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
        FragmentManager  manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Promocije");

        Bundle bundle = new Bundle();
        bundle.putString("href", "http://www.magazinauto.com/category/promocije/");
        bundle.putString("title", "Promocije");
        fragmentMenuPages.setArguments(bundle);
    }

    else if (id == R.id.svet) {

        FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
        FragmentManager  manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Svet");

        Bundle bundle = new Bundle();
        bundle.putString("href", "http://www.magazinauto.com/category/svet/");
        bundle.putString("title", "Svet");
        fragmentMenuPages.setArguments(bundle);

    }

    else if (id == R.id.istorija)  {
        FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title); // OTHER FRAGMENTS
        Title.setText("Istorija");

        Bundle bundle = new Bundle();
        bundle.putString("href", "http://www.magazinauto.com/category/istorija/");
        bundle.putString("title", "Istorija");
        fragmentMenuPages.setArguments(bundle);

    }
    else if (id == R.id.tehnika)  {
        FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Tehnika");

        Bundle bundle = new Bundle();
        bundle.putString("href", "http://www.magazinauto.com/category/tehnika/");
        bundle.putString("title", "Tehnika");
        fragmentMenuPages.setArguments(bundle);
    }

    else if (id == R.id.transport) {
        FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Transport");

        Bundle bundle = new Bundle();
        bundle.putString("href", "http://www.magazinauto.com/category/transport/");
        bundle.putString("title","Transport");
        fragmentMenuPages.setArguments(bundle);

    }
    else if (id == R.id.moto)  {

        FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Moto");

        Bundle bundle = new Bundle();
        bundle.putString("href", "http://www.magazinauto.com/category/moto/");
        bundle.putString("title","Moto");
        fragmentMenuPages.setArguments(bundle);
    }

    else if (id == R.id.lifeStyle)  {

        FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView title = (TextView) view.findViewById(R.id.actionbar_title);
        title.setText("Lifestyle");

        Bundle bundle = new Bundle();
        bundle.putString("href", ("http://www.magazinauto.com/category/life-style/"));

        bundle.putString("title","Lifestyle");
        fragmentMenuPages.setArguments(bundle);
    }

    else if (id == R.id.daliznate) {
        FragmentDaLIPage fragmentMenuPages = new FragmentDaLIPage();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Da li znate?");


        Bundle bundle = new Bundle();
        bundle.putString("href", "http://www.magazinauto.com/category/da-li-znate/");
        bundle.putString("title","Da li znate?");
        fragmentMenuPages.setArguments(bundle);
    }

    else if (id == R.id.redakcija)  {
        RedakcijaFragment redakcijaFragment = new RedakcijaFragment();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                ,redakcijaFragment, redakcijaFragment.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);

        Title.setText("Redakcija");

    }

    else if (id == R.id.kontakt)  {
        KontaktFragment kontaktFragment = new KontaktFragment();

        FragmentManager manager = getSupportFragmentManager();

        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                ,kontaktFragment, kontaktFragment.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Kontakt");
    }

    else {
        return super.onOptionsItemSelected(item);
    }

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

}

Upvotes: 1

Views: 3305

Answers (4)

Balwinder SIngh
Balwinder SIngh

Reputation: 1961

you need to set primaryNavigationFragment to FragmentA. and later from any fragment you can call

activity.supportFragmentManager.primaryNavigationFragment?.childFragmentManager?.fragments?.firstOrNull()

this should return Fragment A

Upvotes: 0

Bawa
Bawa

Reputation: 876

This doesn't directly answers your question, because am not quiet sure what you trying to achieve here. So adding a piece of code below, which might help you to reach a work-around for the problem(maybe).

While transaction you can set your fragment as primary fragment by using the below method call:

fragmentTransaction.setPrimaryNavigationFragment(yourFragment);

Upvotes: 0

JavadBadirkhanly
JavadBadirkhanly

Reputation: 645

Look at the getFragmentManager().popBackStack() methods (there are several to choose from)

Bonus: You can also use this trick ;)

getFragmentManager().popBackStackImmediate(); 

Upvotes: 0

Dishonered
Dishonered

Reputation: 8831

You need to override this method

 @Override
public void onBackPressed()
{
  if(getFragmentManager().getBackStackEntryCount() > 0)
      getFragmentManager().popBackStack();
  else
    super.onBackPressed(); 
}

and dont forget to call addToBackStack(null) on fragment transactions.

Upvotes: 1

Related Questions