Passing data between Fragments in the same Activity

前端 未结 7 1444
清歌不尽
清歌不尽 2021-02-09 12:01

Am working in a project with an Activity that host many fragments. Now I need to share some data (integers, strings, arraylist) between these fragments.

First time i use

相关标签:
7条回答
  • 2021-02-09 12:24

    You have some options:

    Options 1: If you need to share information between fragments, you can use the SharedPreferences to store the information. This is an example:

    SharedPreferences settings = context.getSharedPreferences("Preferences", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("string1", var);
        editor.putBoolean("bool1", var);
        ...
        editor.commit();
    

    Option 2: If you have instantiated a fragment, you can create a set method to store some kind of information in the fragment. For example, if you want to pass an array of string, you can create an array variable in the fragment (with setter method). Later, when you have instantiated this fragment, you can use the setter method to pass this array to the fragment.

    0 讨论(0)
  • 2021-02-09 12:24

    You can create a Application class ( i mean extend Application ) then take some variable what you need , make getter and setter method. set value to variable and get value when and where you need. if you wish i can give you some demo code for it .

    0 讨论(0)
  • 2021-02-09 12:28

    The easiest way to do this is to use Bundle. You do something like this:

    Fragment A:

    Bundle b = new Bundle();
    b.putString("Key", "YourValue");
    b.putInt("YourKey", 1);
    
    FragmentB fragB = new FragmentB();
    fragB.setArguments(b); 
    getFragmentManager().beginTransaction().replace(R.id.your_container, fragB);
    

    Fragment B:

    Bundle b = this.getArguments();
    if(b != null){
       int i = b.getInt("YourKey");
       String s =b.getString("Key");
    }
    

    This is the easiest way that I have found to send data from one fragment to another. Hope it helps someone.

    0 讨论(0)
  • 2021-02-09 12:34

    1.Use BroadCastReceiver (a little complicated)

    2.Use SharePreference

    3.save data into MyApplication extends Application

    0 讨论(0)
  • 2021-02-09 12:37

    We can share the Data between fragments in 2 ways.

    1. By Using Bundle

    2. By using Persistence Storage

    By Using Bundle:-

    Bundle bundle=new Bundle();
    bundle.putString("firstName",Fist);
    bundle.putString("LastName",lastN);
    

    Second Persistence Storage :- this you can check how to store Data in Persistence Storage. Hope it will work for you

    0 讨论(0)
  • 2021-02-09 12:44

    Use a SharedViewModel proposed at the official ViewModel documentation

    First implement fragment-ktx to instantiate your viewmodel more easily

    dependencies {
    implementation "androidx.fragment:fragment-ktx:1.2.2"
    }
    

    Then, you just need to put inside the viewmodel the data you will be sharing with the other fragment

    class SharedViewModel : ViewModel() {
        val selected = MutableLiveData<Item>()
    
        fun select(item: Item) {
            selected.value = item
        }
    }
    

    Then, to finish up, just instantiate your viewModel in each fragment, and set the value of selected from the fragment you want to set the data

    Fragment A

    class MasterFragment : Fragment() {
    
        private val model: SharedViewModel by activityViewModels()
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            itemSelector.setOnClickListener { item ->
            model.select(item)
          }
    
        }
    }
    

    And then, just listen for this value at your Fragment destination

    Fragment B

    class DetailFragment : Fragment() {
    
            private val model: SharedViewModel by activityViewModels()
    
            override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
                super.onViewCreated(view, savedInstanceState)
                model.selected.observe(viewLifecycleOwner, Observer<Item> { item ->
                    // Update the UI
                })
            }
        }
    

    This answer can help you, https://stackoverflow.com/a/60696777/6761436

    0 讨论(0)
提交回复
热议问题