3

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 2

4

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

6
  • Thanks for the reply. Any documentation you know of that can guide me in the right direction? Jan 24, 2013 at 9: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. Jan 24, 2013 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 : stackoverflow.com/questions/10407159/… and developer.android.com/reference/android/app/…
    – baboo
    Jan 24, 2013 at 10:14
  • You confused me a little bit. Should I use the Bundle extras code in startActivityForResult()?? Jan 24, 2013 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:(stackoverflow.com/questions/2679250/…) now in ActivityA handle the result (which is yes bundle extras thing) in activityA onActivityResult method. in onactivityresult open appropriate fragment
    – baboo
    Jan 24, 2013 at 10:30
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.