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 8915705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:02:05+00:00 2026-06-15T05:02:05+00:00

I have some problems passing data from an activity to fragments in it. I

  • 0

I have some problems passing data from an activity to fragments in it. I searched around but didn’t find an answer which suit my situation well.
I have 2 fragment class named CurrentFragment.java and HistoryFragment.java. I initialize them as tabs in an Activity.

    Tab tab = actionBar.newTab()
            .setText(R.string.tab_current)
            .setTabListener(new TaskitTabListener<CurrentFragment>(
                    this, "current", CurrentFragment.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab()
            .setText(R.string.tab_history)
            .setTabListener(new TaskitTabListener<HistoryFragment>(
                    this, "history", HistoryFragment.class));
    actionBar.addTab(tab);

I was told to use setArguments in the Activity and getArguments in the fragments. But in this situation how do I get fragment objects in the Activity? I can’t use getFragmentManager().findFragmentById() since the fragments are added programmatically.

Also, I find some posts saying that I may use getActivity() in fragments to access data in the Activity container, but for me it keep returning null. Does anyone has a working example of that?

  • 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-15T05:02:06+00:00Added an answer on June 15, 2026 at 5:02 am

    [EDIT] I’ve updated my answer to better respond to your question.

    You can also retrieve fragments by tag with getFragmentManager().findFragmentByTag("tag"). Be careful though, if the tab has not been viewed yet the fragment will not exist.

    CurrentFragment curFrag = (CurrentFragment)
        getFragmentManager().findFragmentByTag("current");
    if(curFrag == null) {
        // The user hasn't viewed this tab yet
    } else {
        // Here's your data is a custom function you wrote to receive data as a fragment
        curFrag.heresYourData(data)
    }
    

    If you want the fragment to pull the data from the activity have your activity implement an Interface defined by the fragment. In the onAttach(Activity activity) lifecycle function for fragments you get access to the activity that created the fragment so you can cast that activity as the Interface you defined and make function calls. To do that put the interface in your fragment like this (You can also make the interface its own file if you want to share the same interface among many fragments):

    public interface DataPullingInterface {
        public String getData();
    }
    

    Then implement the the interface in your activity like this:

    public class MyActivity extends Activity implements DataPullingInterface {
        // Your activity code here
        public String getData() {
            return "This is my data"
        }
    }
    

    Finally in your onAttach(Activity activity) method in CurrentFragment cast the activity you receive as the interface you created so you can call those functions.

    private DataPullingInterface mHostInterface;
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if(D) Log.d(TAG, "onAttach");
        try {
            mHostInterface = (DataPullingInterface) activity;
        } catch(ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement DataPullingInterface");
        }
        String myData = mHostInterface.getData();           
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I seem to have a problem passing some strings on from one form to
I have some problems with OpenCV s cvCanny(...) and the Image data types it
I'm a novice developer for WP. I have some problems with parsing json data.
I have this problem.. I'm receiving data from a client..Using a socket connection. But
I've been having problems passing parameters from one .jsp to another. I have two
I'm a novice developer for WP. I have some problems with parsing json data.
i have some problems with a Query seem IN dosen't work with Group_concat, that
I have some problems with Javascript. In fact, I'm just newbie in that script
I have some problems with header() function. It works and doesn't work at the
I have some problems with my first Open Graph custom action: I've created a

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.