Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragments and Broadcast Receivers

I have an activity with two fragments. I am not using <fragment/> tags, I have two classes that extends Fragment, in that fragment, I have:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.bfragment, container, false); // this will inflate the Fragment in activity.
    }

Now the problem is, I am receiving some broadcast receivers in activity from which some receivers update the UI from first fragment and some updates the UI from 2nd.

One of my broadcast receiver defined in my main acitivity is:

private BroadcastReceiver bcReceived = new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, Intent intent) {
            Log.d("", "BC Object Received");

            ActionBar actionbar = getActionBar();
            actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            ActionBar.Tab bTab = actionbar.newTab().setText("B");
            Fragment fragment = new BFragment();
            bTab.setTabListener(new MyTabsListener(fragment));
            actionbar.addTab(bTab, true);

            final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.bTable);  // Getting null pointer exception here. linearLayout is not getting initialized.

I want to use the above linearLayout and use it to inflate a view in it. But getting NPE.

here, when some broadcast receivers update the first fragment, it works properly, but when a broadcast receiver updates the 2nd fragment from activity, I get NPE.

My question is: How and where should I update the fragment? Should it be inside my activity? if yes then in which method? if not then where should I update the fragment?

Please help me!!!

like image 639
Shrikant Ballal Avatar asked Jun 12 '12 08:06

Shrikant Ballal


People also ask

What are broadcast receivers?

A broadcast receiver is an Android component that allows an application to respond to messages (an Android Intent ) that are broadcast by the Android operating system or by an application.

What are broadcast receivers used for?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.

What are the fragments in Android?

A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.

What is the difference between a broadcast receiver and an intent filter?

An IntentFilter specifies the types of intents to which an activity, service, or broadcast receiver can respond to by declaring the capabilities of a component. BroadcastReceiver does not allows an app to receive video streams from live media sources.


1 Answers

Your activity logic should be separated from your fragments logic.

Your activity is supposed to handle the logic like:

I need to display this fragment instead of that one

But your activity is not supposed to handle this kind of logic:

I need to update what's inside the fragment

It is the responsability of the fragment to update it's content. On the other hand, the activity may tell the fragment that it needs to update itself.

With that in mind, your fragments should expose methods like

updateContent(With Blabla)

OR

updateContent()

In your activity, when the BroadcastReceiver receives something you should:

  • Check which fragment is currently displayed
  • Prepare the content to update in the fragment
  • Ask the fragment to update with the updateContent(With Blabla) method.

OR

  • Check which fragment is currently displayed
  • Ask the fragment to update itself with the updateContent() method.

Chose the most simple method according to your application business logic.

like image 135
Timothée Jeannin Avatar answered Oct 21 '22 05:10

Timothée Jeannin