Want To Update Data In Second Fragment With Sharepreferences But Second Fragment Is Not Updating
I am making a calculator app and want to update history in second fragment but not able to find a perfect solution for updating ListView in second fragment. I tried many solutions
Solution 1:
I found out solution for this problem. we can pass data through fragments using Shared ViewModel, no need to use SharedPreferences. we can set text to ViewModel and fetch it in second fragment, even when both fragments are in resumed condition(both are in resumed condition because here we are using simple ViewPager).
I am posting my code below for reference, how I updated data in second fragment.
Add this code in MainActivity.java file. adding both fragments is necessary if you only add any one it will not have the slide effect of ViewPager.
getSupportFragmentManager().beginTransaction().add(R.id.viewPager, new CalculatorFragment()).add(R.id.viewPager, new HistoryFragment()).commit();
Setting text to ViewModel in CalculatorFragment.java file.
publicvoidEquals() {
String calc = etCalc.getText().toString();
if (calc.split("\\+").length == 2) {
String[] calculation = calc.split("\\+");
StringAns = String.valueOf(Double.parseDouble(calculation[0]) +
Double.parseDouble(calculation[1]));
etCalc.setText(Ans);
viewModel.setText(Ans);
}
//to send data to ViewModelpublicvoidonActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
viewModel = ViewModelProviders.of(Objects.requireNonNull(getActivity())).get(SharedViewModel.class);
}
}
Make a class named SharedViewModel.java
package com.shashank.calculator;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
publicclassSharedViewModelextendsViewModel {
privatefinal MutableLiveData<CharSequence> text = newMutableLiveData<>();
publicvoidsetText(CharSequence input) {
text.setValue(input);
}
public LiveData<CharSequence> getText() {
return text;
}
}
and at last getting data from ViewModel in second HistoryFragment.java
publicclassHistoryFragmentextendsFragment {
ArrayList<HistoryList> historyLists;
HistoryAdapter adapter;
SharedViewModel viewModel;
StringHistory;
View rootView;
publicViewonCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_history, container, false);
listView = rootView.findViewById(R.id.history);
historyLists = newArrayList<>();
return rootView;
}
publicvoidonActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// getting data from SharedViewModel and setting it to ListView
viewModel = ViewModelProviders.of(Objects.requireNonNull(getActivity())).get(SharedViewModel.class);
viewModel.getText().observe(getViewLifecycleOwner(), charSequence -> {
History = String.valueOf(charSequence);
adapter = newHistoryAdapter(Objects.requireNonNull(getActivity()), historyLists);
historyLists.add(0,History);
listView.setAdapter(adapter);
}
});
}
Post a Comment for "Want To Update Data In Second Fragment With Sharepreferences But Second Fragment Is Not Updating"