I know we can move between activities via an intent call. But is there a way to move from one activity to a particular fragment hosted by an activity? For example if I have an activity A which hosts 2 fragments f1 and f2,is there a way to move from another activity say B to fragment f2 directly via intent call? Thanks in advance.
2 Answers
hmm it is not possible directly via intent call, you will have to start activity A and via an extra/bundle in intent you can specify to activity A to open fragment f2, meaning you will have to write the logic yourself... not possible directly .. Cheers Try this in Activity B it will open ActivityA and its sending a string which you can check in that activity, based on that string you will add fragment:
Intent i = new Intent(this, ActivityA.class);
i.putExtra("toOpen", "fragment 1");
startActivity(i);
and in ActivityA oncreate
Bundle extras = getIntent().getExtras();
String toOpen = extras.getString("toOpen");
check toOpen string and open appropriate fragment.. Are you asking how to show/add fragments as well , because thats a complete new ball game :) but this would be a nice starts for managing fragments http://developer.android.com/guide/components/fragments.html

- 1,983
- 17
- 23
-
Thanks for the reply. Any documentation you know of that can guide me in the right direction? – user1951690 Jan 24 '13 at 09:10
-
I already have the fragments, I just need a way to show a particular one when I press back button from Activity B. I'll try this. – user1951690 Jan 24 '13 at 10:12
-
oh you need to show with backbutton press , :) that changes alot .. for that use startactivityforresult in activity A , when opening activity B from activity A.. and in activity B setResult example : http://stackoverflow.com/questions/10407159/android-how-to-manage-start-activity-for-result and http://developer.android.com/reference/android/app/Activity.html#StartingActivities – baboo Jan 24 '13 at 10:14
-
You confused me a little bit. Should I use the Bundle extras code in startActivityForResult()?? – user1951690 Jan 24 '13 at 10:23
-
yea sry abt the confusion let me clear it again. you have activityA with 2 fragments, and from activityA you open activityB, after coming back from activityB to activityA you want to open a fragment in activityA.. for this use startActivityForResult in activityA and in activityB you will have to setResult when user presses back btn for that use:(http://stackoverflow.com/questions/2679250/setresult-does-not-work-when-back-button-pressed) now in ActivityA handle the result (which is yes bundle extras thing) in activityA onActivityResult method. in onactivityresult open appropriate fragment – baboo Jan 24 '13 at 10:30
-
Ok Thanks. Will try and revert back. – user1951690 Jan 24 '13 at 10:41
When u open a fragment from a activity on a button click the write this code
Fragment myfragment;
myfragment = new Your_Fragment_Name();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.container, myfragment);
fragmentTransaction.commit();
In my code an id is used: container
. This is the id of layout in which Fragment is open.
if u also send a value from activity to with the fragment then
Bundle bundle = new Bundle();
bundle.putInt("value",value );
and
myfragment.setArguments(bundle);
this line is add after create the FragmentManager object
This code is very helpful

- 3,596
- 4
- 24
- 46

- 25
- 1