How to call Specific Fragment in Activity from Another Activity or Fragment

0
474

How to call Specific Fragment in Activity from Another Activity or Fragment

When you create your Intent, you can give it an extra that determines the fragment to load.

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

// Now start your activity
startActivity(i);

Now, inside your activity check the extra and load the right Fragment:

OnCreate(){
    ...

    int intentFragment = getIntent().getExtras().getInt("frgToLoad");

    switch (intentFragment){
        case FRAGMENT_A:
            // Load corresponding fragment
            break;
        case FRAGMENT_B:
            // Load corresponding fragment
            break;
        case FRAGMENT_C:
            // Load corresponding fragment
            break;
    }
}

Download Android Studio

For more information visit our Blog Website

LEAVE A REPLY

Please enter your comment!
Please enter your name here