Skip to content Skip to sidebar Skip to footer

How To Switch Between Fragments Of Two Different Activity Class

I have two different activities, A & B. Both have NavigationDrawer , look alike, but not are the same, because I could not get the drawer layout ID of activity A in activity B

Solution 1:

I have created an intent inside the second activity and started the activity A with information about the fragment to be called.

Intent i = newIntent(this, ActivityClass.class);
i.putExtra("frgToLoad", "FRAGMENT_A");
startActivity(i);

Now, inside activity A, checked the extra and load the right Fragment:

OnCreate(){
...
if (getIntent().getExtras() != null) {
StringintentFragment= getIntent().getExtras().getString("frgToLoad");

switch (intentFragment){
    case"FRAGMENT_A":
        // Load corresponding fragmentbreak;
    case"FRAGMENT_B":
        // Load corresponding fragmentbreak;
    case"FRAGMENT_C":
        // Load corresponding fragmentbreak;
   }
}
}

One have to check if intent is null or not before I try to assign a value to intentFragment. This is because that line of code is called whether I am coming from activity B or not, and it will throw error if intent is null.

credit : https://stackoverflow.com/a/36064344/3380537

Post a Comment for "How To Switch Between Fragments Of Two Different Activity Class"