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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:50:19+00:00 2026-06-15T10:50:19+00:00

I have one activity – MainActivity . Within this activity I have two fragments,

  • 0

I have one activity – MainActivity. Within this activity I have two fragments, both of which I created declaratively within the xml.

I am trying to pass the String of text input by the user into Fragment A to the text view in Fragment B. However, this is proving to be very difficult. Does anyone know how I might achieve this?

I am aware that a fragment can get a reference to it’s activity using getActivity(). So I’m guessing I would start there?

  • 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-15T10:50:20+00:00Added an answer on June 15, 2026 at 10:50 am

    Have a look at the Android developers page:
    http://developer.android.com/training/basics/fragments/communicating.html#DefineInterface

    Basically, you define an interface in your Fragment A, and let your Activity implement that Interface. Now you can call the interface method in your Fragment, and your Activity will receive the event. Now in your activity, you can call your second Fragment to update the textview with the received value

    Your Activity implements your interface (See FragmentA below)

    public class YourActivity implements FragmentA.TextClicked{
        @Override
        public void sendText(String text){
            // Get Fragment B
            FraB frag = (FragB)
                getSupportFragmentManager().findFragmentById(R.id.fragment_b);
            frag.updateText(text);
        }
    }
    

    Fragment A defines an Interface, and calls the method when needed

    public class FragA extends Fragment{
    
        TextClicked mCallback;
    
        public interface TextClicked{
            public void sendText(String text);
        }
    
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
    
            // This makes sure that the container activity has implemented
            // the callback interface. If not, it throws an exception
            try {
                mCallback = (TextClicked) activity;
            } catch (ClassCastException e) {
                throw new ClassCastException(activity.toString()
                    + " must implement TextClicked");
            }
        }
    
        public void someMethod(){
            mCallback.sendText("YOUR TEXT");
        }
    
        @Override
        public void onDetach() {
            mCallback = null; // => avoid leaking, thanks @Deepscorn
            super.onDetach();
        }
    }
    

    Fragment B has a public method to do something with the text

    public class FragB extends Fragment{
    
        public void updateText(String text){
            // Here you have it
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to display ListView . I have created one activity , in
I have one activity in which I am having one TabHost with two tabs.
So, my application have one activity ProjectActivity as a home, which contains 3 fragments
I have one Activity which displayed some Dialog boxes and Progress bars. This activity
i have one activity in my manifest.xml my activity is CPohonApp but in this
I have following problem: I have one activity in which I have two tabs
I have one activity, off this I have two classes. When my app starts
I need your help. I have one Activity with two fragments: one fragment with
I have one activity which is having linear layout and inside this layout there
I am using fragments to show images/pages.I have one Activity(Main) which contains all the

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.