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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With