How to handle android rotate screen if I have a fragment that observe the data from view model to fill recyclerview list ?! when rotation happened the data lost, so how can I handle it with viewmodel so the user didn't feel any different after rotation?
this is the part of the view model the return data:
private fun getImageList(keyWord: String) {
responseManager.loading()
val disposable = imageListUseCase.execute(keyWord, { success ->
responseManager.hideLoading()
_observeImageListData.value = Event(success)
}, { error ->
responseManager.failed(error)
})
compositeDisposable.add(disposable)
}
and here I set the recycler data on the fragment:
UPDATE
@AndroidEntryPoint
class ImagePickerFragment : Fragment() {
private lateinit var imagePickerBinding: FragmentImagePickerBinding
private val imagePickerViewModel: ImagePickerViewModel by activityViewModels()
private lateinit var imagePickerAdapter: ImagePickerAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
imagePickerBinding = FragmentImagePickerBinding.inflate(inflater, container, false)
observeImageList()
observeSearchText()
return imagePickerBinding.root
}
private fun observeImageList() {
imagePickerViewModel.observeImageListData.observe(
viewLifecycleOwner,
EventObserver { imageList ->
imagePickerAdapter = ImagePickerAdapter(imageList, imagePickerViewModel)
imagePickerBinding.apply {
rvImageList.setHasFixedSize(true)
rvImageList.layoutManager = GridLayoutManager(requireContext(),2)
rvImageList.adapter = imagePickerAdapter
}
})
}
Any tips?
UPDATE :
The viewModel extends from BaseViewModel :
abstract class BaseViewModel: ViewModel() {
val compositeDisposable = CompositeDisposable()
override fun onCleared() {
super.onCleared()
compositeDisposable.clear()
}
}
and this is the full viewmodel:
@HiltViewModel
class ImagePickerViewModel @Inject constructor(
private val imageListUseCase: ImageListUseCase,
private val responseManager: ResponseManager
) : BaseViewModel() {
private val _observeImageListData = MutableLiveData<Event<ArrayList<Image>>>()
init {
getImageList(Constants.KEY_WORD)
}
private fun getImageList(keyWord: String) {
responseManager.loading()
val disposable = imageListUseCase.execute(keyWord, { success ->
responseManager.hideLoading()
_observeImageListData.value = Event(success)
}, { error ->
responseManager.failed(error)
})
compositeDisposable.add(disposable)
}
fun filterSearchKeyWord(filteredKeyWord: String) {
if(filteredKeyWord.isNotEmpty())
getImageList(filteredKeyWord)
}
//getters:
val observeImageListData: LiveData<Event<ArrayList<Image>>>
get() = _observeImageListData
}
CodePudding user response:
In the Activity Tag of manifest file, you should have to mention
<activity
android:name="com.example.ListActivity"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation">
If you are using Android 2.3(API level 13 ) and above use
<activity
android:name="com.example.Activity"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
CodePudding user response:
In your manifest, try putting this in your activity tag:
android:configChanges="orientation"
So it looks like:
<activity
android:name=".MainActivity"
android:configChanges="orientation"
android:exported="true">