Home > OS >  share argument between all fragments
share argument between all fragments

Time:03-23

I am making a bottom navigation app and want to share data between all fragments, for example clicking a button to share a name for all action bars. With safeArgs I can only send data to one fragment. And I want to ask, is any solutions for global arguments(global variables) like redux in react native or provider in flutter?

Thank you

CodePudding user response:

  • You can use shareviewmodel, it's best practice
  • Link refer: viewmodel

CodePudding user response:

EDITED

Just create a public variable as a class variable in your MainActivity

public class MainActivity extends AppCompatActivity{

   public int myVar; // outside of all methods
   ...

Then you have access it everywhere in your Fragments inside that MainActivity, like this:

MainActivity mainActivity = ((MainActivity) getActivity());

if (mainActivity != null)
    mainActivity.myVar = 15;
  • Related