Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I transfer data from one fragment to another fragment android

One way I know that is through activity.We can send data from fragment to activity and activity to fragment Is there any other way.

like image 929
skybolt Avatar asked Oct 12 '13 10:10

skybolt


People also ask

How do you pass data from one fragment to another fragment in Kotlin?

Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Create two FragmentActivity and add the codes which are given below.

How do I move an object from one fragment to another in Android?

Implements the override methods of Parcelable class. You can use Keyboard shortcut as: put cursor over Parcelable class and tap 'Alt, Enter' then click on Implement methods. This is how you Pass data to another Fragment. You can write this piece of code on the event where you want to replace the fragment.

How do I move a bundle from one fragment to another?

Here are some possibilities: Use putInt(), putBoolean(), putString(), putChar(), putByte(), putBooleanArray(), etc. Create a new instance of the fragment to which you would like to send the bundle. Use the setArguments() method to send the bundle to the fragment.


1 Answers

To pass data from one fragment to another Bundle will help.

LifeShapeDetailsFragment fragment = new LifeShapeDetailsFragment(); //  object of next fragment
Bundle bundle = new Bundle();
bundle.putInt("position", id);
 fragment.setArguments(bundle);

Then push/call next Fragments.

and code to next Fragment:

Bundle bundle = this.getArguments();
int myInt = bundle.getInt("position", 0);
like image 120
RobinHood Avatar answered Oct 01 '22 10:10

RobinHood