Android Fragment - BroadcastReceiver call fragment method

David Faizulaev

I have a BroadcastReceiver which receives broadcast sent to a Fragment. I'm getting the broadcast but how can I call a method from the Fragment itself? I basically need to update a List once the broadcast arrives, the List & update method are part of the Fragment.

public class FragmentReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(action != null && action.equals("AllTasksFragmentUpdate"))
        {
            //
        }
    }
}

Registering the receiver:

    @Override
public void onCreate(final Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    getActivity().registerReceiver(new FragmentReceiver(), new IntentFilter("AllTasksFragmentUpdate"));
}

How can I call the method from the Fragment?

dex

You can implement your broadcast reciever in the following way:

import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.adobe.libs.connectors.R;

public class YourFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,
                                @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        //Start listening for refresh local file list   LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mYourBroadcastReceiver,
                new IntentFilter(<YOUR INTENT FILTER>));

        return inflater.inflate(R.layout.your_fragment_layout, null, true);
    }

    @Override
    public void onDestroyView() {
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mYourBroadcastReceiver);
    }

    private final BroadcastReceiver mYourBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Now you can call all your fragments method here
        }
    };
}

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 Call Fragment Method from Fragment

From Dev

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

From Dev

Android - Trying to call parent method from fragment

From Dev

(Android) how to call method in fragment from fragmentActivity

From Dev

Call fragment method in an activity?

From Dev

Call method inside a fragment

From Dev

Unable to call BroadcastReceiver from AsyncTask of fragment

From Dev

Interface in BroadcastReceiver call only activity instead of fragment

From Dev

Call method in fragment from another fragment/activity

From Dev

call fragment method but fragment not prepared complitily

From Dev

Call method in fragment from another fragment/activity

From Dev

call method from fragment to fragment ( refresh adapter )

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 a fragment inside onAdClosed() method

From Dev

Call Fragment method from Activity

From Dev

Call SQLiteOpenHelper method from fragment

From Dev

Call method in MainActivty from Fragment

From Dev

how to call a fragment method in an activity

From Dev

Call method of interface implements with child fragment From container activity Android

From Dev

Android Fragment : which life cycle method to use for web service call

From Dev

Call method of interface implements with child fragment From container activity Android

From Dev

Android - How to call a fragment from inside a fragment?

From Dev

Xamarin Android call a Fragment inside a Fragment

From Dev

Call activity from fragment in android

From Dev

Call activity from fragment in android

From Dev

Call method in child fragment in parent fragment from activity

From Dev

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

Related Related

  1. 1

    Android Call Fragment Method from Fragment

  2. 2

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

  3. 3

    Android - Trying to call parent method from fragment

  4. 4

    (Android) how to call method in fragment from fragmentActivity

  5. 5

    Call fragment method in an activity?

  6. 6

    Call method inside a fragment

  7. 7

    Unable to call BroadcastReceiver from AsyncTask of fragment

  8. 8

    Interface in BroadcastReceiver call only activity instead of fragment

  9. 9

    Call method in fragment from another fragment/activity

  10. 10

    call fragment method but fragment not prepared complitily

  11. 11

    Call method in fragment from another fragment/activity

  12. 12

    call method from fragment to fragment ( refresh adapter )

  13. 13

    Call an activity method from a fragment

  14. 14

    Call a Fragment method from an Adapter

  15. 15

    Call Fragment method from Activity

  16. 16

    Call a fragment inside onAdClosed() method

  17. 17

    Call Fragment method from Activity

  18. 18

    Call SQLiteOpenHelper method from fragment

  19. 19

    Call method in MainActivty from Fragment

  20. 20

    how to call a fragment method in an activity

  21. 21

    Call method of interface implements with child fragment From container activity Android

  22. 22

    Android Fragment : which life cycle method to use for web service call

  23. 23

    Call method of interface implements with child fragment From container activity Android

  24. 24

    Android - How to call a fragment from inside a fragment?

  25. 25

    Xamarin Android call a Fragment inside a Fragment

  26. 26

    Call activity from fragment in android

  27. 27

    Call activity from fragment in android

  28. 28

    Call method in child fragment in parent fragment from activity

  29. 29

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

HotTag

Archive