Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

android - Passing data between fragments contained in an activity

I have an activity A with 3 fragments. Each fragments replaces each other, hence at a given time only 1 is visible.

HomeFragment has 2 textviews wrapped inside 2 cardviews. Each cardview represents a text value which comes from Fragment1 and Fragment2. When I click on say Card1,I get to the Fragment1.

Fragment1 has some cardviews, when I selects any of them I navigate back to HomeFragment and update the cardview text based on my selection in Fragment1.Here is the switch statement, depending upon what card user selects I put that in a bundle and pass it to HomeFragment.

 switch (v.getId()) {
        case R.id.card_view0:

            Fragment1Bundle.putString("Test", "Testing");
            bundle.putBundle("Fragment1Bundle", Fragment1Bundle);
            fragmentTransaction.setCustomAnimations(R.anim.slideup, R.anim.slidedown, R.anim.slideup, R.anim.slidedown);
            fragmentTransaction.replace(R.id.content_frame, fragment);
            fragment.setArguments(bundle);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

            break;

Fragment2 has same behavior as Fragment 1.

switch (v.getId()) {
        case R.id.card_view0:

            Fragment2Bundle.putString("Test2", "Tetsing");
            bundle.putBundle("Fragment2Bundle", Fragment2Bundle);
            fragmentTransaction.setCustomAnimations(R.anim.slideup, R.anim.slidedown, R.anim.slideup, R.anim.slidedown);
            fragmentTransaction.replace(R.id.content_frame, fragment);
            fragment.setArguments(bundle);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

            break;

My challenge is that I am using bundles to pass data between fragments, My home fragment gets updated with the data it from fragment1 but when I go to fragment 2 and after making the selection come back to Home fragment, my fragment1 data is set to default. This is what I am doing in Home Fragments onCreateView()

  try {
          bundle1 = getArguments().getBundle("Fragment1Bundle");
          bundle2 = getArguments().getBundle("Fragment2Bundle");


          tv.setText(bundle1.getString("Test") == null ? null : bundle1.getString("Test"));

            tv2.setText(bundle2.getString("Test2") == null ? nul : bundle2.getString("Test2"));

   } catch (NullPointerException e) {
        Log.d(TAG, e.printStackTrace());
    }

I know that I am creating a new Homefragment in my fragment transaction in both fragment1 and fragment2, How can I keep just 1 instance of Home fragment around.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Another design recommended by Google is to use the main Activity and 2 fragments (in your case Fragment1 and Fragment2). I can see your problem of passing data bundle to HomeFragment. This suggested design uses MainActivity which is declared static (may be required for scoping issue). And it uses an interface to be established between Activity and a Fragment. I think the interface is easier than passing bundle back to the HomeFragment.

A Google webpage is @ Communicating with Other Fragments. This is not just my opinion. A good SO link, I think, is How to pass data between fragments.

Code snippet from the webpage...

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);
   }
...

An example of Activity to Fragment communication:

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
    }
}

Note:

  • OnHeadlineSelectedListener is the interface created by the Fragment.
  • The created method onArticleSelected has a parameter position, which comes from the ListView in ListFragment (in the sample).
  • You can still set data bundles and send data between Activity and Fragment. However I have not sent back data from Fragment to Activity. I normally use Fragment to handle much of UI updates.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...