简体   繁体   中英

fragment communication between two fragments in different activities

I am trying to learn fragment communication in android, I have learnt interfragment communication between two fragments in a single activity using the example from developers site here


I want to know how to communicatate between fragments in two different activities, ive posted a snapshot below

在此处输入图片说明


  • Can someone show a sample program

  • are there any standard developers examples out there, if true please point me to that source any other online docs, blogs that explain this concept crisply

note :: i am aware of knowledge of communicating between two activities using intents when fragments are not involved

A fragment class inside an activity is usually public static. That's why its possible to launch the fragment from another activity.

FragmentTransaction transaction = getFragmentManager().beginTransaction();

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

// Commit the transaction
transaction.commit();

you have fragment 1 and if u want to open the fragment 2 means

  • u have to pass the intent to the activity 2 and from that activity only you have to start the fragment right.
  • so if want to pass the data from the fragment 1 to fragment 2 means you have to send data from fragment 1 to activity 2 ans pass again the data to activity 2 to fragment 2 while creating the fragment 2 from activity 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM