Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send data from Activity to Fragment already created

I only have found information about how to create a Fragment sending some data to it, but only in its instantiation with the constructor.

But I want to know if it is possible to send some data (for instance, two Double objects) to a Fragment from an Activity without having to create a new instance of the Fragment.

A Fragment that has been previously created.

like image 773
sergio Avatar asked Jul 06 '17 23:07

sergio


People also ask

How pass data from activity to fragment in Android using intent?

This example demonstrates how do I pass a variable from activity to Fragment in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I send data from activity to fragment in Viewpager?

The updateFragmetData method will be called in the current selected viewpager item fragment. You can also add a tag parameter to updateFragmetData so that you can be sure the right fragment instance is called. If required you can call isVisible in updateFragmetData implementation to make sure the fragment is visible.


3 Answers

Just add a method in Fragment which you want to receive arguments, then invoke the method in Activity.

Activity's Code:

Activity's Code

Fragment's Code:

Fragment's Code

like image 76
Will Tang Avatar answered Nov 12 '22 05:11

Will Tang


You can transfer any data through a bundle like below :

Bundle bundle = new Bundle();
bundle.putInt(key, value);
your_fragment.setArguments(bundle);

Then in your Fragment, retrieve the data (e.g. in onCreate() method) with:

Bundle bundle = this.getArguments();
if (bundle != null) {
        int myInt = bundle.getInt(key, defaultValue);
}
like image 41
Karim Avatar answered Nov 12 '22 07:11

Karim


The easiest way to do this is to define an interface in the Fragment and implement it within the activity. This link should provide a detailed example on how this can be accomplished. https://developer.android.com/training/basics/fragments/communicating.html

I think the key part you are looking for is here:

ArticleFragment articleFrag = (ArticleFragment)
      getSupportFragmentManager().findFragmentById(R.id.article_fragment);

if (articleFrag != null) {
    // If article frag is available, we're in two-pane layout...

    // Call a method in the ArticleFragment to update its content
    articleFrag.updateArticleView(position);
} else {
    // Otherwise, we're in the one-pane layout and must swap frags...

    // Create fragment and give it an argument for the selected article
    ArticleFragment newFragment = new ArticleFragment();
    Bundle args = new Bundle();
    args.putInt(ArticleFragment.ARG_POSITION, position);
    newFragment.setArguments(args);

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    // Replace whatever is in the fragment_container view with this fragment,
    // and add the transaction to the back stack so the user can navigate back
    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack(null);

    // Commit the transaction
    transaction.commit();
}

First try to retrieve the fragment by calling findFragmentById(R.id.fragment_id), and if it is not null you can make a call to the method you defined in your interface to send some data to it.

like image 27
Zach Sogolow Avatar answered Nov 12 '22 06:11

Zach Sogolow