I have multiple fragments in sliding tab layout. i want to pass data from 1st tab to other tab. please provide code. while passing data tab should swipe from 1st tab to 2nd tab. please help me. thanks in advance
-
https://stackoverflow.com/questions/54763977/how-do-i-pass-a-custom-object-to-a-list-in-a-different-fragment/54764097#54764097 check this out – Salman Khan Mar 13 '19 at 09:17
-
https://stackoverflow.com/a/42870193/9060917 check this – Praveen Mar 13 '19 at 09:26
-
You can do it with an interface. http://www.devexchanges.info/2015/02/android-communicating-between-viewpager.html – MorZa Mar 13 '19 at 09:30
-
Possible duplicate of [pass data to another fragment by swipe view with tab android studio,not button](https://stackoverflow.com/questions/32983033/pass-data-to-another-fragment-by-swipe-view-with-tab-android-studio-not-button) – Android Mar 13 '19 at 09:32
-
@SalmanKhan i'm not getting – Pritham Bnr Mar 13 '19 at 09:46
-
Where are you getting stuck @PrithamBnr? – Salman Khan Mar 13 '19 at 09:52
-
created custom object class. how to pass my edit text values to that. and how to retrieve in fragment b – Pritham Bnr Mar 13 '19 at 10:02
-
You can make your model class singleton so that on tab change you save that data to your singleton model class and then you can get that on last tab. Make sure to make your singleton class empty on last tab button click. – MashukKhan Mar 13 '19 at 10:47
1 Answers
Considering using ViewModel (https://developer.android.com/topic/libraries/architecture/viewmodel) (https://developer.android.com/topic/libraries/architecture/adding-components)
First, Open the ROOT build.gradle file for your project (not the ones for your app or module) and add the google() repository as shown below:
allprojects {
repositories {
google()
jcenter()
}
}
Then, in your APP MODULE build.gradle just simply Add this Library to your Gradle then you can Start Using ViewModel in your project:
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0" // Its Version May Vary
To use ViewModel :
Create ViewModel Class and Define that Data you wanted to Pass
public class MyViewModel extends ViewModel { // Assume you wanted to Pass the Data of 'name' from 1st Tab to 2nd Tab String name = ""; void resetData() { // Function that will Reset the Data name= ""; } }
Let's say 1st Tab is FragmentA, 2nd Tab is FragmentB and you wanted to Pass the 'name' Data when you Swipe from 1st Tab to 2nd Tab
Now, In FragmentA you can Set the 'name' Data of ViewModel before you pass the data to FragmentB
public class FragmentA extends Fragment { private MyViewModel myViewModel ; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // MUST Initialize your ViewModel myViewModel = ViewModelProviders.of(getActivity()).get(MyViewModel.class); } protected void onStart() { super.onStart(); // You can Set your Value anywhere, Let Set the Value at onStart as an Example // Set your Name Data to "Pritham Bnr" myViewModel.name= "Pritham Bnr" } }
Note: Whenever you wanted to Set the 'name' Data of the ViewModel, just use code of myViewModel.name= "Value you want to Set"
Now you can get the 'name' data from the ViewModel in FragmentB When you Swipe from 1st Tab (FragmentA) to 2nd Tab (FragmentB)
public class FragmentB extends Fragment { private MyViewModel myViewModel ; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // MUST Initialize your ViewModel myViewModel = ViewModelProviders.of(getActivity()).get(MyViewModel.class); } protected void onStart() { super.onStart(); // You can Get your Value anywhere by call "myViewModel.name", Let Get the Value at onStart as an Example // Using Toast to Display the 'name' Data pass from FragmentA using ViewModel Toast.makeText(getContext(), myViewModel.name, Toast.LENGTH_SHORT).show(); } // for Example, If you want Clear the Data when Swipe Back to FragmentA, you can call resetData() function of the ViewModel // Let say we Clear the Data when the Fragment onStop() as you Swipe back to FragmentA // This is Optional, just an Example Telling you how to Reset the Data if you want to protected void onStop() { super.onStop(); // Reset Data when Screen is Being Swipe to FragmentA // After Call this function, the ViewModel previous Data of "Pritham Bnr" will be Reset and become "" empty value. // So FragmentA now will get "" Data from the ViewModel myViewModel.destroyViewModelData(); } }
Note: Whenever you wanted to Get the 'name' Data of the ViewModel, just use code of myViewModel.name
ViewModel not only can Store String, Int, Double, etc DataType, it can also store Object which is very helpful when Passing Large Amount of Data from Fragment to Fragment.
This is one of the Simple Way to Pass Data from Fragment to Fragment.
Hope this would help, Thank you.

- 1,570
- 4
- 21
- 36
-
-
-
Added more Details to my Answer, Check the Top Paragraph. @PrithamBnr – I am a Student Mar 13 '19 at 11:55
-
1i ll accept your answer. my target compile sdk version is 26. so i cant use it – Pritham Bnr Mar 14 '19 at 13:12