I am currently doing on a project that requires me to move my data between tabs/fragments.. Let's say the user clicks on the listView item, they will move to another tab, instead of staying in the same tab. May I know how can I achieve that? Can someone help to solve my query? Thank you!
Asked
Active
Viewed 52 times
2 Answers
0
You can trasnfer data between tabs by set and get Arguments, Here is Example
FragmentTwo fragmentTwo = new FragmentTwo();
Bundle bundle = new Bundle();
bundle.putString("key1", "data1");
bundle.putString("key2", "data2");
bundle.putString("key3", "data3");
fragmentTwo.setArguments(bundle);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, fragmentTwo);
fragmentTransaction.commit();

Vishal Chhodwani
- 2,567
- 5
- 27
- 40
0
There are 3 ways to do this
1) Use Interfaces -use interface to pass data objects. Meshy solution
public interface onDataCHange{
public void updateData(String data);
}
2) Use Activity Class - Store model object in activity class and set and get using activity Instance. Quick and Dirty solution
//Get
Object dataModel = (ContainerActivity) getActivity()).getData();
//Set
((ContainerActivity) getActivity()).setData(dataModel );
3) Clean architecture - Center repository hold model objects. View update model via Singleton Center repository object. Single copy of data flow between throughout the App.
@Singleton
public class UserDataRepository implements UserRepository {
private final UserDataStoreFactory userDataStoreFactory;
private final UserEntityDataMapper userEntityDataMapper;
/**
* Constructs a {@link UserRepository}.
*
* @param dataStoreFactory A factory to construct different data source implementations.
* @param userEntityDataMapper {@link UserEntityDataMapper}.
*/
@Inject
UserDataRepository(UserDataStoreFactory dataStoreFactory,
UserEntityDataMapper userEntityDataMapper) {
this.userDataStoreFactory = dataStoreFactory;
this.userEntityDataMapper = userEntityDataMapper;
}
@Override public Observable<List<User>> users() {
//we always get all users from the cloud
final UserDataStore userDataStore = this.userDataStoreFactory.createCloudDataStore();
return userDataStore.userEntityList().map(this.userEntityDataMapper::transform);
}
@Override public Observable<User> user(int userId) {
final UserDataStore userDataStore = this.userDataStoreFactory.create(userId);
return userDataStore.userEntityDetails(userId).map(this.userEntityDataMapper::transform);
}
}

Community
- 1
- 1

Hitesh Sahu
- 41,955
- 17
- 205
- 154