How to Pass Bundle from Fragment to Fragment

How to transfer some data to another Fragment?

Use a Bundle. Here's an example:

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);

Bundle has put methods for lots of data types. See this

Then in your Fragment, retrieve the data (e.g. in onCreate() method) with:

Bundle bundle = this.getArguments();
if (bundle != null) {
int myInt = bundle.getInt(key, defaultValue);
}

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");
}

Pass data between two fragments by bundle kotlin

this is because you create new WordFragment here replace(R.id.searchFragment, WordFragment()) instead of putting the one you added the bundle to it.

How to pass and get value from fragment and activity

Here is the Android Studio proposed solution (= when you create a Blank-Fragment with File -> New -> Fragment -> Fragment(Blank) and you check "include fragment factory methods").

Put this in your Fragment:

class MyFragment: Fragment {

...

companion object {

@JvmStatic
fun newInstance(isMyBoolean: Boolean) = MyFragment().apply {
arguments = Bundle().apply {
putBoolean("REPLACE WITH A STRING CONSTANT", isMyBoolean)
}
}
}
}

.apply is a nice trick to set data when an object is created, or as they state here:

Calls the specified function [block] with this value as its receiver
and returns this value.

Then in your Activity or Fragment do:

val fragment = MyFragment.newInstance(false)
... // transaction stuff happening here

and read the Arguments in your Fragment such as:

private var isMyBoolean = false

override fun onAttach(context: Context?) {
super.onAttach(context)
arguments?.getBoolean("REPLACE WITH A STRING CONSTANT")?.let {
isMyBoolean = it
}
}

Enjoy the magic of Kotlin!

How to pass bundle into fragment using getSupportFragmentManager?

you can set the bundle in the fragment using the setArguments on fragment instance. you can find more info in the docs.
for your example:

Fragment fragment = RecyclerViewFragment.newInstance();

Bundle bundle = new Bundle();
bundle.putString("url", url);

fragment.setArguments(bundle);

getSupportFragmentManager()
.beginTransaction()
.add(R.id.content, fragment)
.commit();

and in your onCreate or onCreateView in the fragment use:

String url =  getArguments().getString("url")

Answered question in the stackoverflow: setArguments - getArguments

Passing and receiving an entire bundle(object) itself from fragment of some activity to another activity

here I see that you have received the bundle of your Dashboard activity into a fragment bundle = this.getArguments();.

And from this you have shared this bundle to your another activity here :- intent.putExtra("bundle", bundle);.

Instead of this you can simply use (if you are not using your bundle anywhere else in the fragment or else bundle() will also work instead of this.getArguments())

if (this.getArguments != null) {
intent.putExtras(this.getArguments())
}

Please note use intent.putExtras instead of intent.putExtra.

so now in your Dashboard Activity you will get bundle using this :-

Bundle extrasBundle = getIntent().getExtras()

Now you can extract all the data from this extrasBundle using extrasBundle.getString("YOUR_KEY");



Related Topics



Leave a reply



Submit