How to transfer some data to another Fragment?

前端 未结 10 2026
说谎
说谎 2020-11-22 11:41

How to transfer some data to another Fragment likewise it was done with extras for intents?

10条回答
  •  Happy的楠姐
    2020-11-22 12:38

    Use a Bundle. Here's an example:

    Fragment fragment = new Fragment();
    Bundle bundle = new Bundle();
    bundle.putInt(key, value);
    fragment.setArguments(bundle);
    

    Bundle has put methods for lots of data types. See this

    Then in your Fragment, retrieve the data (e.g. in onCreate() method) with:

    Bundle bundle = this.getArguments();
    if (bundle != null) {
            int myInt = bundle.getInt(key, defaultValue);
    }
    

提交回复
热议问题