-1

I have this kind of structure

  1. MainPage Activity1 has view pager holding 3 fragments
  2. I am using tabapdater
  3. The first of the fragments have method to move to another Activity2

I want to fire some method in the first fragment when button is clicked in Activity2

Can i do that? can you guys give me any example code for it??

Thanks guys

Kisung Tae
  • 75
  • 1
  • 3

2 Answers2

0

There are some solutions. The first one I think is startActivityForResult. If you start your second activity by calling startActivityForResult instead of startActivity you can expect the result in first activity by overriding the onActivityResult method and then change the first fragment content from there.

Another good and easy solution is using Buses like EventBus and otto. They are well documented.

Mohammad Zarei
  • 1,773
  • 14
  • 33
0

In your Fragment class:

public void foo(Object objectToPassFromActivity) {
    // do what you want
}

In Your activity:

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentByTag(getFragmentTag(R.id.yourViewPagerId, fragmentPosition);
fragment.foo(objectToPassFromActivity)

Add method:

private String getFragmentTag(int viewPagerId, int fragmentPosition) {
    return "android:switcher:" + viewPagerId + ":" + fragmentPosition;
}

You can see the link that i added in comments to get more details.

T.S
  • 911
  • 8
  • 24