Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Sharing between Fragments and Activity in Android

Ive asked a similar question before and didn't get an answer and seems many other ppl are searching for an answer. So I am posting this question to hopefully get a clear answer that everyone can benefit from.

I have an activity with 2 fragments in it. I want fragment2 to set a boolean variable in Activity when a checkbox is checked so that fragment1 can know if the checkbox was checked.

This is my Code:

Activity:

public class modestab extends Activity{     public static Context appContext;      public boolean lf=false;      public void onCreate(Bundle savedInstanceState){         appContext=this; super.onCreate(savedInstanceState); ActionBar tabbar= getActionBar();         tabbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);         ActionBar.Tab ModesTab =tabbar.newTab().setText("Modes");         ActionBar.Tab CatTab =tabbar.newTab().setText("Categories");          Fragment ModesFragment =new modes();         Fragment CatFragment =new cats();          ModesTab.setTabListener(new MyTabsListener(ModesFragment));         CattTab.setTabListener(new MyTabsListener(CatFragment));          tabbar.addTab(ModesTab);         tabbar.addTab(CatTab);       } 

Fragment 1:(Where I want to read the boolean lf set in Acitivity above:

@TargetApi(11) public class tabmodes extends Fragment{ @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)     { View V=inflater.inflate(R.layout.tab_modes, container, false); button1.setOnClickListener(new View.OnClickListener() {              @Override             public void onClick(View v) {                 // TODO Auto-generated method stub                 if(lf==false) //lf here is the lf in Activity which I want to get 

Fragment 2: Where I want to set lf in Activity

..... lifecheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){               @Override                 public void onCheckedChanged(CompoundButton buttonView,                         boolean isChecked) {                   if(lifecheck.isChecked())                       getActivity().lf=true;//Where I want to set the lf flag in Activity                   ;               }           }); 

The code doesn't compile and I am not knowing how to set lf in the Activity nor how to read it. Someone suggested I do getActivity() but I am not able to see the variable.

I tried to create a function setlf(boolean jk) but also I am not able to see it...

Any help is welcome :)

like image 922
Syntax_Error Avatar asked Nov 18 '12 23:11

Syntax_Error


People also ask

How do you pass data between fragments and activities?

So, to pass data from the MotherActivity to such a Fragment you will need to create private Strings/Bundles above the onCreate of your Mother activity - which you can fill with the data you want to pass to the fragments, and pass them on via a method created after the onCreate (here called getMyData()).

Can a ViewModel be shared between activity and fragment?

In android, we can use ViewModel to share data between various fragments or activities by sharing the same ViewModel among all the fragments and they can access everything defined in the ViewModel. This is one way to have communication between fragments or activities.


1 Answers

Many ways :

1) Activity -> Fragment

  • In your Activity : create a bundle and use fragment.setArguments(bundle)
  • in your Fragment : use Bundle bundle = getArguments()

2) Activity -> Fragment

  • In your Fragment : create a public method

  • In your Activity : call an active fragment public method :

    getSupportFragmentManager().findFragmentById(R.id.your_fragment).publicMethod(args)

3) Fragment -> Activity

  • In your Fragment : create un interface with getter and setter methods (callback methods)
  • In your Activity : implement the interface

4) Fragment -> Activity

  • In your Activity : Create public getter and setter or other methods
  • In your Fragment : called public activity getter, setter or other methods using :

    getActivity().getSomething(), getActivity().setSomething(args) or getActivity().someMethod(args)

like image 191
Alain Beauvois Avatar answered Sep 28 '22 11:09

Alain Beauvois