17

I need to pass data between from 5 fragments to one Activity, those fragments send data one after another when i reach 5'th fragment then i need to store all 5 fragments data how can we do this. any idea is Great.enter image description here

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31
Androi Developer
  • 287
  • 1
  • 4
  • 15

8 Answers8

59

Pass data from each fragment to activity, when activity gets all data then process it. You can pass data using interfaces.

Fragment:

public class Fragment2 extends Fragment {

  public interface onSomeEventListener {
    public void someEvent(String s);
  }

  onSomeEventListener someEventListener;

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
        try {
          someEventListener = (onSomeEventListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement onSomeEventListener");
        }
  }

  final String LOG_TAG = "myLogs";

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment2, null);

    Button button = (Button) v.findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        someEventListener.someEvent("Test text to Fragment1");
      }
    });

    return v;
  }
}

Activity:

public class MainActivity extends Activity implements onSomeEventListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Fragment frag2 = new Fragment2();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(R.id.fragment2, frag2);
        ft.commit();
    }

  @Override
  public void someEvent(String s) {
      Fragment frag1 = getFragmentManager().findFragmentById(R.id.fragment1);
      ((TextView)frag1.getView().findViewById(R.id.textView)).setText("Text from Fragment 2:" + s);
  }
}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
Kalyaganov Alexey
  • 1,701
  • 1
  • 16
  • 23
  • Hey Kalyanganov Alexey can you elobarate me how to pass data using interfaces if it is with a simple example appreciated... – srujan maddula Apr 03 '14 at 05:41
  • 1
    Shure. Google have good examples http://developer.android.com/training/basics/fragments/communicating.html – Kalyaganov Alexey Apr 03 '14 at 08:53
  • but if I only have one button which is in the last fragment, how can I do that? http://stackoverflow.com/questions/32953477/pass-data-to-fragment – Hoo Oct 06 '15 at 16:01
13

The following link explains the design for communication between fragments.

Communicating with Other Fragments

To allow a Fragment to communicate up to its Activity, you can define an interface in the Fragment class and implement it within the Activity. The Fragment captures the interface implementation during its onAttach() lifecycle method and can then call the Interface methods in order to communicate with the Activity.

Here is an example of Fragment to Activity communication:

public class HeadlinesFragment extends ListFragment {

OnHeadlineSelectedListener mCallback;

// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
    public void onArticleSelected(int position);
}

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

...
}

Now the fragment can deliver messages to the activity by calling the onArticleSelected() method (or other methods in the interface) using the mCallback instance of the OnHeadlineSelectedListener interface.

For example, the following method in the fragment is called when the user clicks on a list item. The fragment uses the callback interface to deliver the event to the parent activity.

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

Implement the Interface

In order to receive event callbacks from the fragment, the activity that hosts it must implement the interface defined in the fragment class.

For example, the following activity implements the interface from the above example.

public static class MainActivity extends Activity
    implements HeadlinesFragment.OnHeadlineSelectedListener{
...

public void onArticleSelected(int position) {
    // The user selected the headline of an article from the HeadlinesFragment
    // Do something here to display that article
}
}

Deliver a Message to a Fragment

The host activity can deliver messages to a fragment by capturing the Fragment instance with findFragmentById(), then directly call the fragment's public methods.

For instance, imagine that the activity shown above may contain another fragment that's used to display the item specified by the data returned in the above callback method. In this case, the activity can pass the information received in the callback method to the other fragment that will display the item:

public static class MainActivity extends Activity
    implements HeadlinesFragment.OnHeadlineSelectedListener{
...

public void onArticleSelected(int position) {
    // The user selected the headline of an article from the HeadlinesFragment
    // Do something here to display that article

    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();
    }
   }
 }
Shiva
  • 697
  • 1
  • 4
  • 11
  • I did like this, when i click, iPhone inthat fragment i have some button and edit text then when i submit i am comming to main screen. again when i click rest of the list items i am performing same operations at the end when i click All Info then i need to send iPhone,BlackBerry,Android,Nokia data to different activities. I don't know how to do this.(http://stackoverflow.com/questions/14439941/passing-data-between-fragments-to-activity) – Androi Developer Jan 22 '13 at 03:32
  • Can you be more elaborate on the problem you are facing? What is it that you are trying to achieve? – Shiva Jan 22 '13 at 13:38
8

I tried all the above and it didnt work for me. This is how i made it work.I used interface as means to send data from fragment to activity.

FragmentToActivity.java

public interface FragmentToActivity {
void communicate(String comm);

}

FragmentOne

public class FragmentOne extends Fragment {

private FragmentToActivity mCallback;


@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try {
        mCallback = (FragmentToActivity) context;
    } catch (ClassCastException e) {
        throw new ClassCastException(context.toString()
                + " must implement FragmentToActivity");
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_login, container, 
false);
sendData("Andrews");

return v;
}
@Override
public void onDetach() {
    mCallback = null;
    super.onDetach();
}

public void onRefresh() {
    Toast.makeText(getActivity(), "Fragment : Refresh called.",
            Toast.LENGTH_SHORT).show();
  }
private void sendData(String comm)
    {
    mCallback.communicate(comm);

    }

 }


}

Activity One

public class Account extends AppCompatActivity implements 
  FragmentToActivity{

  @Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 }

@Override
public void communicate(String s) {


    Log.d("received", s);
      }


}
6

You have to go back information to your fragment's activity. And your Activity dispatch information to its fragments :

// In fragment A
((ParentActivity)getActivity()).dispatchInformations("test");

// In ParentActivity
public void dispatchInformations(String mesg){
    fragmentB.sendMessage(mesg);
}

This is a basic example

throrin19
  • 17,796
  • 4
  • 32
  • 52
2

You can use Communicator pattern explained in the above answers. Also, You can use RxJava2.for better decoupling and efficiency.

1- Create the bus:

public final class RxBus {

    private static final BehaviorSubject<Object> behaviorSubject
        = BehaviorSubject.create();


    public static BehaviorSubject<Object> getSubject() {
        return behaviorSubject;
    }

}

2- the sender activity or fragment

//the data to be passed
MyData  data =getMyData();
RxBus.getSubject().onNext(data) ;

3-the receiver activity or fragment

private Subscription subscription;

public onCreate(Bundle savedInstanceState){
    subscription = RxBus.getSubject()
                    .subscribe(new Subscriber<Object>() {

            @Override
            public void onNext(Object o) {
                if (o instanceof MyData) {
                    Log.d("tag", (MyData)o.getData();
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
}

4-unSubscribe to avoid memory leacks:

@Override
protected void onDestroy() {
    super.onDestroy();
 if(subscription!=null){
     subscription.unsubscribe();
   }

}
Quick learner
  • 10,632
  • 4
  • 45
  • 55
Bishoy Kamel
  • 2,327
  • 2
  • 17
  • 29
1

There's a really simple way to pass data from a Fragment to another Activity that is not its container one.

1) In the Fragment: When you initiate the activity, say onButtonClick, pass the data you want to pass as an extra in your intent, as such:

     Intent intent = new Intent(getActivity(), MapsActivity.class);
     intent.putExtra("data", dataString);
     startActivity(intent);

2) In the receiving Activity: in your onCreate method, create a Bundle to retrieve the information passed, as such:

Bundle extras = getIntent().getExtras();
    if (extras != null) {
        receivingString = extras.getString("data");
    } else {
        // handle case
    }

Hope it helped :)

Claus Lens
  • 119
  • 1
  • 4
0

I was looking for a solution to passed data from fragments to the activity. This is how I did it and I found it most appropriate for what I needed it.

I found it best to share and update data using a global shared ViewModel

In shared ViewModel, I store and update data using mutable live data and I scope it to the MainActivity. The ViewModel is a singleton and remains in memory until the activity lifecycle ends.

class SharedViewModel: ViewModel() {

    private val selectedItems: MutableLiveData<List<Product>> =
        MutableLiveData<List<Product>>(listOf())

    fun getItems(): LiveData<List<Product>> {
        return selectedItems
    }

    fun sendSelectedItems(items: MutableList<Product>) {
        selectedItems.postValue(items)
    }
}

MainActivity: AppCompactActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    // scope shared view model to MainActivity
    // I can access and update data from here
        val model = ViewModelProvider(this).get(SharedViewModel::class.java)
    }
}

class MyFragment1: Fragment() {
    // I'm able to get and update data in the SharedViewModel in fragment
    // to activity
    private val sharedModel: SharedViewModel by activityViewModels()
}

In addition, you can add callbacks to the main activity. You can use an interface.

interface IAddListener {
    fun sendItems(items: MutableList<Product>?)
}

class MyFragment: Fragment() {
   
   override fun onAttach(context: Context) {
        super.onAttach(context)

        if (context is IAddListener)
            mCallback = context
    }

    private var mCallback: IAddListener? = null
} 

class MainActivity: AppCompactActivity(), IAddToPrintQueueListener {
    
    override fun sendItems(items: MutableList<Product>?) {
        // update something
    }
}

Here is the link to the documentation about sharing data documentation

Liz
  • 41
  • 6
0

The Fragment library provides two options for communication:

  1. A shared ViewModel
  2. The Fragment Result API.

The recommended option depends on the use case. To share persistent data with custom APIs, use a ViewModel. For a one-time result with data that can be placed in a Bundle, use the Fragment Result API.

Please read this https://developer.android.com/guide/fragments/communicate#java

Chanaka Fernando
  • 2,176
  • 19
  • 19