Send Data from Activity to Fragment in Android

Send data from activity to fragment in Android

From Activity you send data with intent as:

Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);

and in Fragment onCreateView method:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("edttext");
return inflater.inflate(R.layout.fragment, container, false);
}

How to pass data from Activity to Fragment using bundle

While creating bundle:

Bundle bundle = new Bundle();
bundle.putString("userId", userId);
Fragment fragment = new Fragment();
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_placeholder_id, dataFragment, "anyTagName").commit();

The To get the data in your fragment:

if (getArguments != null) {
String userId = getArguments().getString("userId");
}

How to pass data from Activity to Fragment with TabLayout

When starting the Fragment you can pass parameters through a bundle

/*ACTIVITY CODE*/
Bundle args = new Bundle();
args.putStringArrayList("argument_name", myListInString);
MyFragment fragment = new MyFragment();
fragment.setArguments(args);
fragment.show(fragmentTransaction, "fragment_tag")

And use them in your new snippet as follows

/* FRAGMENT CODE */
ArrayList<String> myList = getArguments.getStringArrayList("argument_name");
// ... FILL TABLE

How to pass data that changes real-time from activity to fragment?

class MyViewModel : ViewModel() {
private val realtimedata = MutableLiveData<ByteArray>()

val sensorData: LiveData<ByteArray> = realtimedata

fun update(data: ByteArray){
realtimedata.value = data
}
}

class MainActivity: Activity() {

private val viewModel: MyViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

bluetoothSensorCallBack { data ->
// Update the realtimedata
viewModel.update(data)
}
}
}

class SensordisplayFragment : Fragment() {

// Use the 'by activityViewModels()' Kotlin property delegate
// from the fragment-ktx artifact
private val model: MyViewModel by activityViewModels()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
model.sensorData.observe(viewLifecycleOwner, Observer<ByteArray> { data ->
// Update the UI
})
}
}

Send data from Activity to Fragment already created

Just add a method in Fragment which you want to receive arguments, then invoke the method in Activity.

Activity's Code:

Activity's Code


Fragment's Code:

Fragment's Code

How to send data from Activity to Fragment with ViewPager2?

My solution:

MainActivity:

pagerAdapter = new MainPagerAdapter(this);
pagerAdapter.setData(fragment1Container);

MainPagerAdapter:

public class MainPagerAdapter extends FragmentStateAdapter
{
private Fragment1Container fragment1Container;
private Fragment2Container fragment2Container;

public void setData(Fragment1Container container)
{
fragment1Container = container;
}

public void setData(Fragment2Container container)
{
fragment2Container = container;
}

@NonNull
@Override
public Fragment createFragment(int position)
{
switch (position)
{
case 0:
Fragment1 fragment1 = new Fragment1();

Bundle bundle = new Bundle();
Gson gson = new Gson();

bundle.putString("fragment1Container", gson.toJson(fragment1Container));
fragment1.setArguments(bundle);

return fragment1;
case 1:...

Fragment1:

private Fragment1Container fragment1Container;

if (bundle != null)
{
Gson gson = new Gson();
String jsonString = bundle.getString("fragment1Container");
Type entityType = new TypeToken<Fragment1Container>(){}.getType();
fragment1Container = gson.fromJson(jsonString , entityType );
}


Related Topics



Leave a reply



Submit