user4302606
user4302606

Reputation:

How to update view in Fragment from child fragment in android

enter image description here

Have two fragments A and B, Fragment A has Textview and Fragment B has edittext and button. Click on submit in FragmentB need to update textview in FragmentA with Edittext text.

How to do communication between fragment?

Upvotes: 1

Views: 1739

Answers (3)

Rajan Bhavsar
Rajan Bhavsar

Reputation: 1857

You need to interact the activity first which will interact the second fragment. and also read this article on how to do it.

https://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

Upvotes: 1

user4302606
user4302606

Reputation:

n this example, FragmentA call notify. INotifier

public interface INotifier {
    public void notify(Object data);
}

Utils

public class Utils {
    public static INotifier notifier;
}

FragmentA

public FragmentA extends Fragment {

   public void onCreateView(...) {

   }

   public void inSomeMethod() {
        if (Utils.notifier != null) {
           Utils.notifier.notify(data);
        }
   }
}

FragmentB

public FragmentB extends Fragment implements INotifier {

   public void onCreateView(...) {
       Utils.notifier = this;   
   }

   @Override
   public void notify(Object data) {
       // handle data
   }
}

Upvotes: 3

Mustansar Saeed
Mustansar Saeed

Reputation: 2790

The communication between Fragments is done usinng Listeners. When you want to update fragment, use the listener to tell the MainActivity to update the second fragment as recommended by Google http://developer.android.com/training/basics/fragments/communicating.html. Create the interface in Fragment and Implement this in Activity

Listener in Fragment

public interface FragmentUpdateInterface {
        void updateFragment(String newText);
}

@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 = (FragmentUpdateInterface ) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement FragmentUpdateListener");
    }
}

@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Send the event to the host activity
        mCallback.updateFragment("New Text");
    }

MainActivity Implement fragment in MainActivity as

public static class MainActivity extends Activity
    implements MyFragment.FragmentUpdateListener{

public void updateFragment(String newText) {
OtherFragment otherFrag = (OtherFragment)
                getSupportFragmentManager().findFragmentById(R.id.other_fragment);

        if (otherFrag != null) {
            otherFrag.updateFragment(newText);
        } else {
            // Otherwise, we're in the one-pane layout and must swap frags...

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

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.fragment_container, otherFrag);
            transaction.addToBackStack(null);

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

Hope this helps.

UPDATE:

You can also use LocalBroadcastManager.getInstance().sendBroadcast() to notify to the other fragment as well.

Upvotes: 0

Related Questions