Search code examples
androidandroid-fragmentactivity

how to launch a fragment of an activity from another activity?


I have one activity 'A' which has many buttons on it to call several corresponding fragments of another activity 'B'.

Activity 'B' contains a navigation drawer where i have created fragments for all the items that we have in the navigation drawer.

So, how do i immediately launch suppose 'fragment 1' ( i.e one of the items of the navigation drawer), inside Activity B, when Activity A calls Activity B.


Solution

  • Add an extra to your Intent which defines which Fragment should be loaded.

    intent.putExtra("fragment","FragA");
    

    In your onCreate in Activity B:

    String fragment = getIntent.getStringExtra("fragment");
    // Do something to load correct fragment