When should i remove livedata observer on android? - android

I am observing a list in firestore using LiveData . This observations is dependent on another authentication LiveData.
Should i remove the old LiveData observer before creating the new one? What will happen if i don't?
Currently i am removing the observer using next code but i can simplify it greatly if i won't need to since i do the same all over my code
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
...
//Authentication observer which is the ItemAuto dependent
viewModel.auth.observe(viewLifecycleOwner, Observer {auth ->
updateUserItemAutoLiveData(auth)
})
}
private fun updateUserItemAutoLiveData(auth: Auth) {
if (!auth.uid.isNullOrEmpty()) {
removeUserItemAutoObservers()
itemAutoLiveDate = viewModel.getUserItemAutoLiveData(auth.uid)
itemAutoLiveDate!!.observe(viewLifecycleOwner, Observer {
if (it != null) {
if (it.data != null) {
itemAutoCompleteAdapter.submitItemAuto(it)
}
}
})
} else {
removeUserItemAutoObservers()
}
}
private fun removeUserItemAutoObservers() {
if (itemAutoLiveDate != null && itemAutoLiveDate!!.hasObservers()) {
itemAutoLiveDate!!.removeObservers(this)
}
}
ps: i am using Doug Stevenson tutorial which is great!

If you are using observe method, LiveData will be automatically cleared in onDestroy state.
Observers are bound to Lifecycle objects and clean up after themselves
when their associated lifecycle is destroyed.
More information can be found here
You need to remove livedata manually only if you use observeForever method. The reason why you need to remove it manually is because when you use observeForever method, you don't specify the lifecycle of it.

Related

re call api in fragment android kotlin and perform searchView

the question is similar on Where should I call Rest API in fragment
but i want to discuss about the right on code.
i have call an api from oncreateView()
for example
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
GlobalScope.launch {
showHomeViewModel("someState")
}
}
in function showHomeViewModel() i have defined condition for adapter
private suspend fun showHomeViewModel(state: String) {
withContext(Dispatchers.Main) {
viewModel.liveData().observe(viewLifecycleOwner, observer)
if(adapter.itemCount == 0) callApi() else Log.d(TAG_LOG, "nothing todo")
}
}
so its calling if the data == 0
but i have searchView component , when i navigate to other fragment and back to first fragment , the observer is null...
private val observer =
Observer<MutableList<DataItem>> { item ->
Log.d(TAG_LOG, "observer data $item") // always null if i have ever navigate to other fragment
if (item != null)
adapter.setData(item)
binding.progress.gone()
}
the problem is solve when i remove the condition adapter.itemCount == 0.
but the lifecycle my device always call api when screen or fragment appear.
before i always put the callApi from onResume(), but in my course should i put to the onCreateView and other explain should in onViewCreate() but the main problem is , **its good the code if i use the condition adapter.itemCount == 0 ? to perform searachView , where i defined on viewModel for search (liveData) **
i was tried to from adapter Filterable to perform search but its not work with liveData, so i use my viewModel instead of filterable.

UI will not be updated after LiveData data is changed

Here is my code:
class HomeFragment : Fragment() {
val viewModel by lazy {
ViewModelProvider(this)[HomeViewModel::class.java]
}
private lateinit var adapter: ArticleListAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_home, container, false)
return root
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val linearLayoutManager = LinearLayoutManager(activity)
rv_home.layoutManager = linearLayoutManager
adapter = ArticleListAdapter(viewModel.articleList)
rv_home.adapter = adapter
viewModel.getHomeArticle(0)
viewModel.articleLiveData.observe(viewLifecycleOwner, { result ->
val HomePages = result.getOrNull()
viewModel.articleList = HomePages
adapter.notifyDataSetChanged()
})
}
}
class HomeViewModel : ViewModel() {
var articleList: HomeArticleBean? = null
private val observerArticleLiveData = MutableLiveData<Int>()
val articleLiveData = Transformations.switchMap(observerArticleLiveData) { page ->
Repository.getHomeArticles(page)
}
fun getHomeArticle(page: Int) {
observerArticleLiveData.value = page
}
}
When I call getHomeArticle() from HomeFragment,the program should execute
Repository.getHomeArticles(page)
But after Dubug I found this line of code would not be executed.
This caused my articleLiveData observation in HomeFragment to be invalid, thus preventing me from updating the UI. I'm new to Jetpack so I don't know why this is happening, my code was written after someone else's code and I had no problems running his code, I looked carefully to see if there were differences between my code and his but didn't find any. I am searching for a long time on the net. But no use. Please help or try to give some ideas on how to achieve this. Thanks in advance.
Your problem is with your use of viewModel.articleList:
adapter = ArticleListAdapter(viewModel.articleList)
This creates your adapter and ties its data to the list currently stored at viewModel.articleList (which, for some reason, isn't in your HomeViewModel code?)
viewModel.articleList = HomePages
This line completely throws away the list currently stored at viewModel.articleList and re-assigns the variable to store a completely new list. Your adapter knows nothing about this new list - it still has a reference to the old list.
Therefore you have a couple of choices:
Have your ArticleListAdapter have a setData method that takes the new list. That setData would update the list the ArcticleListAdapter is keeping track of (and, as a benefit, it could also internally call notifyDataSetChanged(), making it is always called.
Update the viewModel.articleList with new entries, rather than replace the whole list, thus ensuring that the list that ArticleListAdapter is using has your updated data:
viewModel.articleLiveData.observe(viewLifecycleOwner, { result ->
val HomePages = result.getOrNull()
viewModel.articleList.clear()
if (HomePages != null) {
viewModel.articleList.addAll(HomePages)
}
adapter.notifyDataSetChanged()
})
Make your ArticleListAdapter extend ListAdapter. This class handles tracking the list of data for you and gives you a simple submitList method which you'd call from your observe method. As a side benefit, it uses DiffUtil which is way more efficient than notifyDataSetChanged().

Kotlin ViewModel onchange gets called multiple times when back from Fragment (using Lifecycle implementation)

I am working with the MVVM architecture.
The code
When I click a button, the method orderAction is triggered. It just posts an enum (further logic will be added).
ViewModel
class DashboardUserViewModel(application: Application) : SessionViewModel(application) {
enum class Action {
QRCODE,
ORDER,
TOILETTE
}
val action: LiveData<Action>
get() = mutableAction
private val mutableAction = MutableLiveData<Action>()
init {
}
fun orderAction() {
viewModelScope.launch(Dispatchers.IO) {
// Some queries before the postValue
mutableAction.postValue(Action.QRCODE)
}
}
}
The fragment observes the LiveData obj and calls a method that opens a new fragment. I'm using the navigator here, but I don't think that the details about it are useful in this context. Notice that I'm using viewLifecycleOwner
Fragment
class DashboardFragment : Fragment() {
lateinit var binding: FragmentDashboardBinding
private val viewModel: DashboardUserViewModel by lazy {
ViewModelProvider(this).get(DashboardUserViewModel::class.java)
}
private val observer = Observer<DashboardUserViewModel.Action> {
// Tried but I would like to have a more elegant solution
//if (viewLifecycleOwner.lifecycle.currentState == Lifecycle.State.RESUMED)
it?.let {
when (it) {
DashboardUserViewModel.Action.QRCODE -> navigateToQRScanner()
DashboardUserViewModel.Action.ORDER -> TODO()
DashboardUserViewModel.Action.TOILETTE -> TODO()
}
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentDashboardBinding.inflate(inflater, container, false)
binding.viewModel = viewModel
binding.lifecycleOwner = this
viewModel.action.observe(viewLifecycleOwner, observer)
// Tried but still having the issue
//viewModel.action.reObserve(viewLifecycleOwner, observer)
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
// Tried but still having the issue
//viewModel.action.removeObserver(observer)
}
private fun navigateToQRScanner() {
log("START QR SCANNER")
findNavController().navigate(LoginFragmentDirections.actionLoginToPrivacy())
}
}
The problem
When I close the opened fragment (using findNavController().navigateUp()), the Observe.onChanged of DashboardFragment is immediately called and the fragment is opened again.
I have already checked this question and tried all the proposed solutions in the mentioned link (as you can see in the commented code). Only this solution worked, but it's not very elegant and forces me to do that check every time.
I would like to try a more solid and optimal solution.
Keep in mind that in that thread there was no Lifecycle implementation.
The issue happens because LiveData always post the available data to the observer if any data is readily available. Afterwords it will post the updates. I think it is the expected working since this behaviour has not been fixed even-though bug raised in issue tracker.
However there are many solutions suggested by developers in SO, i found this one easy to adapt and actually working just fine.
Solution
viewModel.messagesLiveData.observe(viewLifecycleOwner, {
if (viewLifecycleOwner.lifecycle.currentState == Lifecycle.State.RESUMED) {
//Do your stuff
}
})
That's how LiveData works, it's a value holder, it holds the last value.
If you need to have your objects consumed, so that the action only triggers once, consider wrapping your object in a Consumable, like this
class ConsumableValue<T>(private val data: T) {
private val consumed = AtomicBoolean(false)
fun consume(block: ConsumableValue<T>.(T) -> Unit) {
if (!consumed.getAndSet(true)) {
block(data)
}
}
}
then you define you LiveData as
val action: LiveData<ConsumableValue<Action>>
get() = mutableAction
private val mutableAction = MutableLiveData<ConsumableValue<Action>>()
then in your observer, you'd do
private val observer = Observer<ConsumableValue<DashboardUserViewModel.Action>> {
it?.consume { action ->
when (action) {
DashboardUserViewModel.Action.QRCODE -> navigateToQRScanner()
DashboardUserViewModel.Action.ORDER -> TODO()
DashboardUserViewModel.Action.TOILETTE -> TODO()
}
}
}
UPDATE
Found a different and still useful implementation of what Frances answered here. Take a look

Android LiveData and provider/manager pattern

I'm working on an app where a data source is defined in a Provider/Manager. This class (let's call it InfoProvider) is pretty much just a black box - it has some properties, and calls that when executed, result in change of these properties (similar to how a Repository work, but instead of calls returning values, they execute an async call that will result in the change of one or more properties of the provider).
This setup is specifically for Bluetooth Low Energy - we all know how badly managed it is on Android, and I wanted to make it as asynchronous as possible, and use databinding+livedata+viewmodels to achieve a fully responsive architecture.
With Xamarin this would be easy, just define the InfoProvider as a field in the ViewModel, and bind to its fields. However I don't necessarily want to expose all fields in all viewmodels (some might only need the battery status of the device, some might need full access, some might just execute functions without waiting for a response). For functions, it's easy to proxy, but for LiveData<T> I haven't found much information. How would I go forward and "pass around" the LiveData field?
Example:
class InfoProvider {
var batteryPercent = MutableLiveData<Int>()
public fun requestBatteryUpdate() {
[...]
batteryPercent.value = newValue
}
}
// ViewModel for accessing device battery, inheriting from architecture ViewModel
class DeviceBatteryViewModel: ViewModel() {
var batteryPercentage = MutableLiveData<Int>()
val infoProvider: InfoProvider by inject()
init {
// TODO: Subscribe this.batteryPercentage to infoProvider.batteryPercent
fun onButtonClick() {
infoProvider.requestBatteryUpdate()
}
}
class DeviceBatteryFragment: Fragment() {
val ViewModel: DeviceBatteryViewModel by inject()
private lateinit var binding: DeviceBatteryBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
DeviceBatteryBinding.inflate(inflater, container, false).also { binding = it }.root
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.viewModel = this.ViewModel
}
}
// res/layout/fragment_devicebattery.xml
<layout [namespaces]>
<data class="bindings.DeviceBatteryBinding>
<variable android:name="viewModel" type=".DeviceBatteryViewModel />
</data>
<WhatEverLayout [...]>
<TextView [...] android:text="#{viewModel.batteryPercentage.toString()}" />
<Button [...] android:onClick="#{() -> viewModel.onButtonClick()}" />
</WhatEverLayout>
</layout>
What I'd like to avoid is the Rx-style .observe(() -> {}), .subscribe(() -> {}) etc. kind of exchanges. Can this be done (i.e. if I assign the value of infoProvider.batteryPercent to the VM's batteryPercentage field, will it also receive updates), or should I bind directly to the infoProvider?
There is no way to "pass around" the LiveData field without calling batteryPercent.observe(...). Additionally, you will need to use a Lifecycler Owner to Observe the field (unless you want to ObserveForever which is not a recommended solution).
My suggestion would be something like this:
InfoProvider {
val repositoryBatteryUpdate = BehaviorSubject.create<Int>()
fun observeRepositoryBatteryUpdate(): Observable<Int> {
return repositoryBatteryUpdate
}
fun requestBatteryUpdate(){
// Pseudo code for actually update
// Result goes into repositoryBatteryUpdate.onNext(...)
}
}
ViewModel{
val status: MutableLiveData<Int>
init{
repository.observeRepositoryItems()
.subscribe( update -> status.postValue(status))
}
fun update(){
repository.requestBatteryUpdate()
}
}
Fragment{
viewModel.status.observe() // <-- Here you observe Updates
viewModel.update()
}
Note that you will have to dispose the subscription in the ViewModel onCleared.
Note that all of this is pseudo code and it should be done a lot cleaner than this.

Get value from Bottom Sheet Dialog Fragment

I'm starting bottomSheetDialogFragment from a fragment A.
I want to select the date from that bottomSheetDialogFragment then set it in the fragment A.
The select date is already done, I just want to get it in the fragment A to set it in some fields.
How can I get the value?
Any suggestions how to do it?
Create an interface class like this
public interface CustomInterface {
public void callbackMethod(String date);
}
Implement this interface in your Activity or Fragment. and make an object of this Interface.
private CustomInterface callback;
Initialize it in onCreate or onCreateView
callback=this;
Now pass this callback in your BottomSheetDialogFragment constructor when you call it.
yourBottomSheetObject = new YourBottomSheet(callback);
yourBottomSheetObject.show(getSupportFragmentManager()," string");
Now in your BottomSheetFragment's constructor
private CustomInterface callback;
public SelectStartTimeSheet(CustomInterface callback){
this.callback=callback;
}
And at last use this callback object to set your date
callback.callbackMethod("your date");
and yout will recieve this date in your Fragment or Your Activity in callbackMethod function.
override the constructor of a fragment is a bad practice as the document said:
Every fragment must have an
* empty constructor, so it can be instantiated when restoring its
* activity's state.
if you using another constructor that passing a callback as the param, when the fragment is resotored by the framework, your app crash
the recommend way is using viewModel and livedata.
Android navigation architecture component
eg:
Suppose you open Fragment B from Fragment A using navController.
and you want some data from fragment B to Fragment A.
class B :BottomSheetDialogFragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.your_layout, container, false)
root.sampleButton.setOnClickListener {
val navController = findNavController()
navController.previousBackStackEntry?.savedStateHandle?.set("your_key", "your_value")
dismiss()
}
}
and in your Fragment A:
findNavController().currentBackStackEntry?.savedStateHandle?.getLiveData<String>("your_key")
?.observe(viewLifecycleOwner) {
if (it == "your_value") {
//your code
}
}
you can use do as below:
Select Account Fragment code
class SelectAccountFragment(val clickListener: OnOptionCLickListener) : BottomSheetDialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.bottom_fragment_accounts, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val list = DataProcessorApp(context).allUsers
val rvAccounts = view.findViewById<RecyclerView>(R.id.rvAccounts)
rvAccounts.layoutManager = LinearLayoutManager(context)
rvAccounts.adapter = AccountsAdapter(context, list)
Log.e(tag,"Accounts "+list.size);
tvAccountAdd.setOnClickListener {
val intent = Intent(context,LoginActivity::class.java)
startActivity(intent)
}
tvManageAccounts.setOnClickListener {
Log.e(tag,"Manage Click")
clickListener.onManageClick()
}
}
interface OnOptionCLickListener{
fun onManageClick()
}
}
Now show and get call back into another fragment /activity as below
SelectAccountFragment accountFragment = new SelectAccountFragment(() -> {
//get fragment by tag and dismiss it
BottomSheetDialogFragment fragment = (BottomSheetDialogFragment) getChildFragmentManager().findFragmentByTag(SelectAccountFragment.class.getSimpleName();
if (fragment!=null){
fragment.dismiss();
}
});
accountFragment.show(getChildFragmentManager(),SelectAccountFragment.class.getSimpleName());
If you are using BottomSheetDialogFragment , since it's a fragment, you should create your interface and bind to it at onAttach lifecycle method of the fragment , doing the appropriate cast of activity reference to your listener/callback type.
Implement this interface in your activity and dispatch change when someone click in a item of fragment's inner recyclerview, for instance
It's a well known pattern and are explained better at here
One big advice is rethink your app architecture, since the best approach is to always pass primitive/simple/tiny data between Android components through Bundle, and your components are able to retrieve the required state with their dependencies later on.
For example, you should never pass along large Objects like Bitmaps, Data Classes , DTO's or View References.
first there is some serialization process going on regarding Parcel which impacts in app responsiveness
second it can lead you to TransactionTooLarge type of error.
Hope that helps!
You can also use LocalBroadcastManager. And as hglf said, it is better to keep the empty constructor for your fragment and use newInstance(Type value) instead to instantiate your fragment if you still want to use the interface callBack way.
You can use the benefit of Navigation library:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val navController = findNavController();
// After a configuration change or process death, the currentBackStackEntry
// points to the dialog destination, so you must use getBackStackEntry()
// with the specific ID of your destination to ensure we always
// get the right NavBackStackEntry
val navBackStackEntry = navController.getBackStackEntry(R.id.your_fragment)
// Create our observer and add it to the NavBackStackEntry's lifecycle
val observer = LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_RESUME
&& navBackStackEntry.savedStateHandle.contains("key")) {
val result = navBackStackEntry.savedStateHandle.get<String>("key");
// Do something with the result
}
}
navBackStackEntry.lifecycle.addObserver(observer)
// As addObserver() does not automatically remove the observer, we
// call removeObserver() manually when the view lifecycle is destroyed
viewLifecycleOwner.lifecycle.addObserver(LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_DESTROY) {
navBackStackEntry.lifecycle.removeObserver(observer)
}
})
}
For more info, read the document.
The accepted answer is wrong.
What you can do is just user Fragment A's childFragmentManager when calling show().
like this:
val childFragmentManager = fragmentA.childFragmentManager
bottomSheetDialogFragment.show(childFragmentManager, "dialog")

Categories

Resources