Passing data between fragments contained in an act

2019-01-15 11:58发布

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.

2条回答
劫难
2楼-- · 2019-01-15 12:20

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.
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-15 12:35

how to pass values from activity to already open fragment and update array-list help me please. when I using interface the array-list size is zero what I do? do not us bundle method.

public class Main2Activity extends AppCompatActivity{

String desc = "data";
OnDataPassedListener onDataPassedListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    String passedArg = getIntent().getExtras().getString("id");
    Log.d("data",passedArg);
    Scription scription = new Scription();
    onDataPassedListener = (OnDataPassedListener)scription;
    onDataPassedListener.onDataPassed(passedArg,desc);

}
public interface OnDataPassedListener {
    void onDataPassed(String text,String name);
}

}

public class Test extends Fragment implements 

Main2Activity.OnDataPassedListener{

. . . . @Override

public void onDataPassed(String text,String name) {
    monthlylists.get(Integer.valueOf(text)).setFood_type(name);
}
查看更多
登录 后发表回答