How to send Data from Activity to fragments in android? PRIMITIVE DATA using Bundle


Useful video : https://youtu.be/R58MvndRAP0


There are 2 ways to do this :

1] Make a Bundle and pass that to the Fragment (If you use this method you can pass data only when the fragment is being created )

2] Create a user defined method inside the fragment so that you can call that method in the mainactivity and send the data.


1] Make a Bundle approach.


first make a bundle in mainactivity and pass it to the fragment object using setArguments().

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

//user defined button onclick method
public void Show(View view) {

EditText editText = findViewById(R.id.edittext1);
String message =editText.getText().toString();

Bundle bundle= new Bundle();
bundle.putString("keyy",message);


//Creating an object of fragment class
Datafragment datafragment=new Datafragment();

//sets the bundle into the fragment class object
datafragment.setArguments(bundle);

getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,datafragment).commit();
}



second...fetch the Bundle  inside the fragment class using getArguments().




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view =inflater.inflate(R.layout.fragment_datafragment, container, false);


//gets the bundle we passed in the MainActivity
Bundle bundle = getArguments();
if (bundle !=null) {
String message = bundle.getString("keyy");

TextView textView = view.findViewById(R.id.textview1);
textView.setText(message);
}

return view;
}





2]  Creating a user defined method to get Data. (EASY)



first create a method inside the fragment class so when you create an object in the MainActivity , you can pass the method the data.



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_data, container, false);

TextView textView =view.findViewById(R.id.textview1);
textView.setText(message);

return view;
}


public void setData(String m){
this.message=m;
}
}




next,fetch the data inside the fragment and assign it a variable 


(Make sure to remove the code for Bundle if you use this method)

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void show(View view) {

EditText editText =findViewById(R.id.edittext1);
String message = editText.getText().toString();

DataFragment dataFragment=new DataFragment();


//user defined method inside the fragment class that will fetch the data
dataFragment.setData(message);

getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,dataFragment).commit();

}
}








Comments

Popular posts from this blog

React Js + React-Redux (part-2)

React Js + CSS Styling + React Router (part-1)

ViteJS (Module Bundlers, Build Tools)