android-livedata

Some fragment observers trigger after pop from back stack although data is not changed

£可爱£侵袭症+ 提交于 2020-04-17 21:32:52
问题 I have some problem in nested fragment in Kotlin. I have nested fragment with ViewModel. After resuming fragment from back button press all observers on viewModel LiveData triggers again although my data does not changed. First i googled and tried for define observer in filed variable and check if it is initialized then do not observer it again: lateinit var observer: Observer fun method(){ if (::observer.isInitialized) return observer = Observer{ ... } viewModel.x_live_data.observe

LiveData Object keeps being null after getValue() is called

╄→гoц情女王★ 提交于 2020-04-10 09:19:34
问题 I want to update a member variable of an object inside my Repository on a LiveData- Object. The problem is, that if I call the getValue() Method, I keep getting an NullPointerException, although the value does exist inside my Room- Library. My question now is, how do I get the value from the LiveData Object without calling the observe() Method? (I am not able to call the observe method inside my repository, cause that method wants me to enter a LifeCycleOwner- reference, which is not present

LiveData Object keeps being null after getValue() is called

不羁的心 提交于 2020-04-10 09:16:20
问题 I want to update a member variable of an object inside my Repository on a LiveData- Object. The problem is, that if I call the getValue() Method, I keep getting an NullPointerException, although the value does exist inside my Room- Library. My question now is, how do I get the value from the LiveData Object without calling the observe() Method? (I am not able to call the observe method inside my repository, cause that method wants me to enter a LifeCycleOwner- reference, which is not present

how to broadcast Receiver and MVVM?

本小妞迷上赌 提交于 2020-04-07 08:00:07
问题 my manifest <receiver android:name=".ui.receiver.NetworkChangeReceiver" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> </intent-filter> </receiver> and NetworkChangeReceiver Class class NetworkChangeReceiver : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { val connMgr = context?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val

Android : How to write a unit test for fragment depending on a viewmodel Live data attribute?

怎甘沉沦 提交于 2020-03-25 18:57:08
问题 I have a listview in my fragment UI that its elements set depend on status of a value that come from a viewmodel LiveData attribute. I want to create instrumental test for the fragment which englobes 3 scenarios test case related to the value set of that attribute and I don't where to start. My code should kind look like below : class MyViewModel : ViewModel() { var status = MutableLiveData("") } class MyFragment : Fragment() { private lateinit var myViewModel: MyViewModel private lateinit

Refactoring google's NetworkBoundResource class to use RxJava instead of LiveData

孤者浪人 提交于 2020-03-17 07:33:47
问题 Google's android architecture components tutorial here has a part that explains how to abstract the logic of getting data over the network. In it, they create an abstract class called NetworkBoundResource using LiveData to create a reactive stream as the basis for all reactive network requests. public abstract class NetworkBoundResource<ResultType, RequestType> { private final AppExecutors appExecutors; private final MediatorLiveData<Resource<ResultType>> result = new MediatorLiveData<>();

LiveData.getValue returns null values before OnChanged method

狂风中的少年 提交于 2020-03-06 09:20:28
问题 I have a Fragment with a TextView that opens a custom DialogFragment with a RecyclerView list in it. I am using an MVVM structure with a ViewModel, Dao, and Repository to populate the RecyclerView list with a POJO item (Currency). Whenever I install or restart the app, there is a delay in the RecyclerView list being populated. It initially shows the DialogFragment with no data in it and from using the debugger, it seems that it only populates once the onChanged method is called for my

Multiple calls to set LiveData is not observed

陌路散爱 提交于 2020-03-01 02:06:34
问题 I have recently seen a weird issue that is acting as a barrier to my project. Multiple calls to set the live data value does not invoke the observer in the view. It seems that only the last value that was set actually invokes the Observer in the view. Here is the code snippet for a review. MainActivity.kt class MainActivity : AppCompatActivity() { private lateinit var viewModel: MainViewModel override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)

Multiple calls to set LiveData is not observed

家住魔仙堡 提交于 2020-03-01 02:05:15
问题 I have recently seen a weird issue that is acting as a barrier to my project. Multiple calls to set the live data value does not invoke the observer in the view. It seems that only the last value that was set actually invokes the Observer in the view. Here is the code snippet for a review. MainActivity.kt class MainActivity : AppCompatActivity() { private lateinit var viewModel: MainViewModel override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)

Communication between view and ViewModel in MVVM with LiveData

送分小仙女□ 提交于 2020-02-23 15:58:25
问题 What is a proper way to communicate between the ViewModel and the View , Google architecture components give use LiveData in which the view subscribes to the changes and update itself accordingly, but this communication not suitable for single events, for example show message, show progress, hide progress etc. There are some hacks like SingleLiveEvent in Googles example but it work only for 1 observer. Some developers using EventBus but i think it can quickly get out of control when the