Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8064487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:22:50+00:00 2026-06-05T11:22:50+00:00

I have an activity with two fragments . I am not using <fragment/> tags,

  • 0

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!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:22:52+00:00Added an answer on June 5, 2026 at 11:22 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two fragments in an activity where one fragment takes up 70% and
I have an activity that has two tabs and a list view in each.
I need your help. I have one Activity with two fragments: one fragment with
I have an activity made up of two fragments. On the left is a
I have two fragments in an activity: <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=horizontal android:id=@+id/mainlayout android:layout_width=fill_parent android:layout_height=fill_parent> <fragment
I have two Fragment s in my Activity : fragment A with button X
I have two activity groups that contain a list that go to a detail
I have an activity that has two layouts one in the folder layout and
I am using viewpager with action-bar tab and I am have two fragment in
I have two issues with activity indicator: 1. Activity Indicator not showing up on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.