Khalil
Khalil

Reputation: 33

How to navigate to Specific Fragment in NavigationDrawer from an Activity

My problem is that I am using NavigationDrawer with multiple fragments. I have an Activity. I am trying to return from the activity to Specific Fragment. I have tried to send an intent. It works but it always stay in that fragment due to the bundle value is not null. So if you have a way how to make the bundle null or you have a better solution than the intent I will be grateful. Thanks, all.

This Method is capable of switching the Fragments

public void DisplaySelectedScreen(int id) {

        infoNom = this.getIntent().getExtras();
        if (infoNom != null) {
            id = Integer.valueOf(infoNom.getString("Fragment"));
        }

        infoNom = null ; //not Working

        Fragment fragment = null;

        switch (id) {
            case R.id.nav_home:
                fragment = new Home();
                break;
            case R.id.nav_info:
                fragment = new Informations();
                break;
            case R.id.nav_cmd:
                fragment = new Commandes();
                break;
            case R.id.nav_panier:
                fragment = new Cart();
                break;
            case R.id.nav_contact:
                fragment = new Contact();
                break;
            case R.id.nav_about_us:
                fragment = new About_Us();
                break;
            case R.id.nav_logout:
                ParseUser.logOut();
                Intent intent = new Intent(Main.this, Welcome.class);
                startActivity(intent);
                break;

        }

        if (fragment != null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.content_frame, fragment);
            ft.commit();
        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);

    }

Upvotes: 1

Views: 241

Answers (2)

albeee
albeee

Reputation: 1472

Ways to replace extras

void removeExtra(String name) Remove extended data from the intent.

void removeFlags(int flags) Remove these flags from the intent.

Intent replaceExtras(Intent src) Completely replace the extras in the Intent with the extras in the given Intent.

Intent replaceExtras(Bundle extras) Completely replace the extras in the Intent with the given Bundle of extras.

Hope this helps

Upvotes: 0

Vikas Tiwari
Vikas Tiwari

Reputation: 499

You can clear the bundle using below method:

getIntent().removeExtra("key"); 

Hope It help you !

Upvotes: 1

Related Questions