Call method in fragment from another fragment/activity

PePe

I made swipe PageView with fragment like that http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

Fragment2 have function myUpdate() which updates some data in Fragment2 (data from sql to list). I could call it easely in Fragment2, but I also need to run myUpdate() from Fragment1 or from other Activity and i don't know how.

I found a few ways but then need R.id.* or tag and i have no idea where to find it.

  FragmentManager fm = getFragmentManager();
  Fragment2 fragment = (Fragment2) fm.findFragmentByTag(<...>);
  fragment.myUpdate();

or same with findViewById... I suppose I should make some changes in xml, but i don't know what to change... Thanks.

Xaver Kapeller

There are multiple ways to achieve this, one would be local broadcasts. Implementing a BroadcastReceiver for local broadcasts is exactly the same as for normal broadcasts:

private BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if(action != null && action.equals("update")) {
            // perform your update
        }

    }
};

You can register a BroadcastReceiver for your local broadcasts in the first Fragment like this:

LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(context);

IntentFilter intentFilter = new IntentFilter("update");
// Here you can add additional actions which then would be received by the BroadcastReceiver

broadcastManager.registerReceiver(receiver, intentFilter);

and you can send local broadcasts like this:

Intent intent = new Intent("update");

LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(context);
broadcastManager.sendBroadcast(intent);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

(Android) how to call method in fragment from fragmentActivity

From Dev

Call FragmentActivity from Fragment

From Dev

Call method in fragment from another fragment/activity

From Dev

How to call method from another fragment

From Dev

Android - Back button from FragmentActivity to another Fragment

From Dev

how to call method of one fragment from another fragment class in android

From Dev

How to call fragment from activity without using fragmentactivity?

From Dev

How to call a fragment layout from the FragmentActivity to avoid NullPointerException

From Java

Call an activity method from a fragment

From Dev

Call a Fragment method from an Adapter

From Dev

Call Fragment method from Activity

From Dev

Call Fragment method from Activity

From Dev

Call SQLiteOpenHelper method from fragment

From Dev

Call method in MainActivty from Fragment

From Dev

call method from fragment to fragment ( refresh adapter )

From Dev

Android Call Fragment Method from Fragment

From Dev

refresh fragment UI from fragmentActivity

From Dev

Import text from fragmentactivity to fragment

From Dev

refresh fragment UI from fragmentActivity

From Dev

Open new Fragment from FragmentActivity

From Dev

How to call a method in a fragment through the onClick in another fragment?

From Dev

Calling Fragment Method by Parent FragmentActivity, getting NullPointerException

From Dev

How to make method don't call again after user returned to this fragment from another?

From Dev

How to call a fragment from another activity

From Dev

Call dynamic fragment method from activity

From Dev

Call Tabbed Fragment method from Activity

From Dev

How to call method inside the fragment from activity?

From Dev

Call fragment method from activity but view is null

From Dev

failed to call Adapter method from fragment

Related Related

  1. 1

    (Android) how to call method in fragment from fragmentActivity

  2. 2

    Call FragmentActivity from Fragment

  3. 3

    Call method in fragment from another fragment/activity

  4. 4

    How to call method from another fragment

  5. 5

    Android - Back button from FragmentActivity to another Fragment

  6. 6

    how to call method of one fragment from another fragment class in android

  7. 7

    How to call fragment from activity without using fragmentactivity?

  8. 8

    How to call a fragment layout from the FragmentActivity to avoid NullPointerException

  9. 9

    Call an activity method from a fragment

  10. 10

    Call a Fragment method from an Adapter

  11. 11

    Call Fragment method from Activity

  12. 12

    Call Fragment method from Activity

  13. 13

    Call SQLiteOpenHelper method from fragment

  14. 14

    Call method in MainActivty from Fragment

  15. 15

    call method from fragment to fragment ( refresh adapter )

  16. 16

    Android Call Fragment Method from Fragment

  17. 17

    refresh fragment UI from fragmentActivity

  18. 18

    Import text from fragmentactivity to fragment

  19. 19

    refresh fragment UI from fragmentActivity

  20. 20

    Open new Fragment from FragmentActivity

  21. 21

    How to call a method in a fragment through the onClick in another fragment?

  22. 22

    Calling Fragment Method by Parent FragmentActivity, getting NullPointerException

  23. 23

    How to make method don't call again after user returned to this fragment from another?

  24. 24

    How to call a fragment from another activity

  25. 25

    Call dynamic fragment method from activity

  26. 26

    Call Tabbed Fragment method from Activity

  27. 27

    How to call method inside the fragment from activity?

  28. 28

    Call fragment method from activity but view is null

  29. 29

    failed to call Adapter method from fragment

HotTag

Archive