Passing data between fragments contained in an activity

前端 未结 2 665
广开言路
广开言路 2020-12-06 15:32

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 ca

相关标签:
2条回答
  • 2020-12-06 16:30

    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.
    0 讨论(0)
  • 2020-12-06 16:34

    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);
    }
    
    0 讨论(0)
提交回复
热议问题