Search code examples
androidbroadcastreceiverandroid-actionbarandroid-4.0-ice-cream-sandwich

Developing UI with Broadcast Receivers and Tabs in ActionBar


I am using ActionBar Tabs in Android 4. I have 5 Broadcast receivers, and all of them updates the part of UI. There are two Tabs in my app, both the tabs have different UI associated(that is, both tabs have different xml files associated with them, which will be inflated according to tab selection). For each Tab I am inflating a fragment.

Initially, I am showing only one tab. On clicking a button from the UI associated with that tab, I add another tab and I receive 5 broadcast receivers. I get broadcast receivers in any order. Now say there are following broadcast receivers: A B C D E

A, B, D and E updates the UI which is part of Tab 1.   

C updates the UI which is part of Tab 2.

Whenever a new tab is created, its UI gets replaced with previous tab's UI, so when, say A and B gets called, they update the UI which is from Tab 1, but when C is received, the UI is now replaced with Tab2's UI, and it updates the UI, but when D and E is received, they try to update the UI of tab 1, but since tab 2 is now inflated, I get null pointer exception on that statement on which D and E tried to make change.

So please tell me, how should I update my UI according to the broadcasters?

UPDATE: Now, I am getting NPE on following: This is inside one of my receiver, here I am creating new tab and want to insert a table from the broadcast receiver.

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

final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mdsTable); // NPE linearLayout is not getting initialized.

I don't understand why this is happening, since I have implemented MyTabsListener as:

class MyTabsListener implements ActionBar.TabListener {
    public Fragment fragment;

    public MyTabsListener(Fragment fragment) {
        this.fragment = fragment;
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        Toast.makeText(StartActivity.appContext, "Reselected!",
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.replace(R.id.fragment_container, fragment);
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.remove(fragment);
    }
}

Solution

  • Here what I did is, first of all, whenever I receive Broadcast Receivers, I don't update the UI in onReceive() method, instead I used to save the data that I receive into persistent storage.

    And based on the event, I used to create a new Tab. And now when user navigated to new Tab, the onCreateView() is called for that tab and I update the UI in this method.

    So no problem of:

    Whenever a new tab is created, its UI gets replaced with previous tab's UI, so when, say A and B gets called, they update the UI which is from Tab 1, but when C is received, the UI is now replaced with Tab2's UI, and it updates the UI, but when D and E is received, they try to update the UI of tab 1, but since tab 2 is now inflated, I get null pointer exception on that statement on which D and E tried to make change.
    

    since I update the UI in each onCreateView() method for each Tab.