Questions tagged [android-architecture-lifecycle]
The android-architecture-lifecycle tag has no usage guidance.
68
questions
1
vote
1
answer
443
views
How do I use MVVM with bound service?
I have a bound service that is started as foreground service for handling exoplayer.
Here is how my fragment handles the service -
@AndroidEntryPoint
class AudioPlayerFragment: Fragment() {
...
1
vote
0
answers
247
views
how to initialise ViewModel in createFragment of FragmentStateAdapter?
I have a ExerciseDetailFragment that contains an ExerciseFragmentStatePagerAdapter to show a bunch of SupportUsFragmentCard. These cards can be swiped horizontally by the user.
In my ...
11
votes
3
answers
887
views
java.lang.IllegalStateException: Cancel call cannot happen without a maybeRun
I am trying to test a simple ViewModel with Robolectric.
Here's my ViewModel
GreetingsViewModel.kt
@FlowPreview
@ExperimentalCoroutinesApi
class GreetingsViewModel : ViewModel() {
private val ...
0
votes
1
answer
339
views
Get Activity Context from Lifecycle object
I have made my component Lifecycle-aware. I want to use activityContext in that component.
My question is: is there any way to use activityContext from the Lifecycle object?
1
vote
1
answer
818
views
How to inject dependencies in a SavedStateHandle-aware AndroidViewModel?
Assume a view model like this:
public class FooViewModel extends AndroidViewModel {
@Inject public FooViewModel(Application app, SavedStateHandle handle, Bar bar) {
// ...
}
}
I ...
0
votes
0
answers
81
views
OnChanged of MediatorLiveData not called if Transformatons not used
I am following https://github.com/android/architecture-components-samples/tree/88747993139224a4bb6dbe985adf652d557de621/GithubBrowserSample for sample application. I can see that they have used ...
3
votes
1
answer
1k
views
Is LifecycleService deprecated along with lifecycle-extensions artifact?
It seems the LifecycleService class that used to live in the lifecycle-extensions artifact is not available in any of the current recommended artifacts. I'd expect it to be in lifecycle-runtime, which ...
1
vote
1
answer
841
views
LiveData value is null after setting in viewModelScope
I have a viewmodel for a fragment with a search functionality. I made use of Coroutines to fetch from the API and then set the MediatorLiveData value with the result, although the list of objects are ...
3
votes
1
answer
1k
views
App crash on creating ViewModel only when minifyEnabled and with LifeCycle v 2.1.0
My app is crashing on launch with a LinkageError when creating a view model using lazy{}. The crash only happens when:
minifyEnabled is set to true in build.gradle, AND
I use ver. 2.1.0 of ...
13
votes
4
answers
10k
views
ViewModel refetches data when fragment is recreated
I am using Bottom Navigation with Navigation Architecture Component. When the user navigates from one item to another(via Bottom navigation) and back again view model call repository function to fetch ...
2
votes
1
answer
350
views
what does the ::MainViewModel mean when pass to the singleArgViewModelFactory
in kotlin coroutines lab smaple, https://codelabs.developers.google.com/codelabs/kotlin-coroutines/#6
it creates the viewModel by passing the MainViewModel.FACTORY(repository)
val viewModel = ...
1
vote
1
answer
1k
views
Duplicate files copied in APK META-INF/android.arch.lifecycle_runtime.version
I have the following error while building my APK.
Duplicate files copied in APK META-INF/android.arch.lifecycle_runtime.version
File1: /Users/.../build/intermediates/exploded-aar/com.google....
0
votes
1
answer
902
views
android.arch.lifecycle:extensions:1.1.1 directory
I am trying to add lifecycle:extensions to my project but it shows the err
Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve android.arch.lifecycle:extensions:1.1.1.
...
1
vote
1
answer
524
views
Should I share my ViewModel across two fragments?
I'm attempting to follow Android best practices and use the latest recommended architecture components. You can see my attempt so far here: https://github.com/randroid88/TodayILearned
Right now the ...
3
votes
2
answers
12k
views
Forcing Paging Library DataSource refresh
In my ViewModel, I load data using
private val pagingConfig = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setInitialLoadSizeHint(INITIAL_LOAD_SIZE_HINT)
.setPageSize(...
4
votes
0
answers
533
views
Observing LiveData instance from View
I created a ViewGroup that updates it's contents based on the state of a LiveData instance, however i don't know if observing a LiveData instance in a view can cause some problems ?
This is the code ...
6
votes
2
answers
2k
views
Does event wrapper pattern replace the use of SingleLiveEvent?
I'm adopting MVVM to my Android apps recently. In order to solve the problems underlying with the lifecycle of an app, Google had released LiveData.
The usage of LiveData has different scenarios, as ...
-1
votes
1
answer
451
views
Android MVVM. Is life-cycle aware Model a code smell or a proper design? [closed]
For my activity I need to know the connectivity state of the phone.
Since this is the data that my UI needs to react to, it falls into a Model realm of MVVM (please let me know if you disagree).
To ...
2
votes
1
answer
132
views
Cannot trust current state of AAC lifecycle
Issue summary
getLifecycle().getCurrentState() gave me wrong state.
Environment (integrated library, OS, etc)
compileSdkVersion : 27
targetSdkVersion : 27
support library : 27.1.1
Expected ...
5
votes
2
answers
4k
views
Do I need to call removeObserver for lifecycle, upon its onDestroy() event? [duplicate]
This is a very simple question:
Background
I'm using the relatively new Lifecycle class (part of the android architecture components libraries) to handle some events of the Activity/Fragment in an ...
6
votes
0
answers
2k
views
ViewModel, Room, LiveData, RecyclerView filter data
I'm working on an inventory application based on Architecture components (ViewModel/Room/Livedata) and RecyclerView. The app based on the practise that Google recommends in Developer training book.
...
1
vote
0
answers
822
views
Android ViewModel Sharing Between Fragments [duplicate]
My team is just beginning to transition our app over to using ViewModels in Android and need some assistance. Our app has a collection of buildings displayed in a fragment. You can select a building ...
3
votes
1
answer
201
views
Android ViewModel design/architecture
I'm an android beginner, and just looking for some advice with regard to app architecture - specifically relating to using ViewModels. There is no local database, each screen issues a new network ...
0
votes
2
answers
75
views
LifeCycle Aware Codelab Concept
I was learning the Lifecycle Aware Components from the Codelab which starts from Chronometer example. In step 2 I have a doubt. These are the code files for reference
ChronoActivity2.java
package ...
0
votes
0
answers
508
views
Android Java ViewModel with Dagger 2
I'm trying to use viewmodel with dagger 2.
However, on orientation change, ViewModel is being recreated. I get SwipeRefreshLayout NullPointerException on featuredViewModel.getLoading.observe().
Where ...
8
votes
6
answers
9k
views
How can I add unit test for android architecture components life cycle event?
I tried to add a unit test for my function which supports architecture components lifecycle event. To support lifecycle event, I added the @OnLifecycleEvent annotation for my function which I want to ...
2
votes
1
answer
2k
views
Room Livedata.observeforever not working after activity resumes
I have tested some test cases using the simple Room test code as below.
"dao.getAll().observeForever()" does not work after main activity resumes.
new words are inserted correctly but only observation ...
3
votes
1
answer
564
views
What is the difference between Observer and LifecycleObserver?
I am struggling to understand the difference between LiveData being a LifecycleObserver and the Observer that the LiveData object receives in the observe() method.
Am I right saying that the LiveData ...
6
votes
1
answer
3k
views
Filter recycler view populated by view model's LiveData entries
I am using architecture component view model's LiveData to populate recycler view and want to add a searchview filter but couldn't find any solution.
I tried to use filterable interface in the adapter ...
0
votes
1
answer
804
views
Attempt to invoke virtual method 'boolean androidx.work.State.isFinished()' on a null object reference
I am trying to learn WorkManager for Android using codeLab documentation.
I app crash when I try to test functionality of beginUniqueWork(Ensure Unique Work)
I just implement beginUniqueWork and ...
2
votes
0
answers
75
views
Android Architecture Component - Prevent @OnLifecycleEvent when launching camera Intent
I implemented 2 LifecycleEvents Lifecycle.Event.ON_PAUSE and Lifecycle.Event.ON_START now these get triggered whenever I launch the camera Intent (which is logical in some way). Is there any way to ...
9
votes
2
answers
2k
views
Cant find class DiffCallback in Android Architecture component 1.1.1:
Recently I updated android.arch support library version in gradle file
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:1.1.1"
// alternatively, just ViewModel
...
-1
votes
1
answer
366
views
Not able to get the LifeCycle ViewModel Class Using ViewModelProvider
I am trying to get the ViewModel Class for the specific fragment. But ViewModelProvider.of(this).get(UserListViewModel.class) does not work for me. Extending my Fragment from "android.support....
4
votes
0
answers
2k
views
Live Data Observer - Wait for RESUMED State
This is my scenario:
User presses a button
A long slow animation is played
While animation is being played - user presses the home button and
goes to homescreen
The animation completes in background ...
4
votes
2
answers
1k
views
Why Do I Get "Multiple dex files define Landroid/support/v7/recyclerview/extensions/ListAdapter"
I get the dreaded java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex error. The stack trace shows the root cause as:
...
4
votes
2
answers
971
views
Android Lifecycle ViewModel not persisted after locking and unlocking in landscape mode
I have a pretty simple app with a dummy Activity and dummy Android Lifecycle ViewModel ViewModel.
FragmentActivity
class FragmentActivity: AppCompatActivity() {
companion object {
...
2
votes
1
answer
2k
views
How do I create a ViewModel for each page in a ViewPager?
I have a ViewPager, where I show multiple instances of PetFragment. To create a new instance of a PetFragment, I call PetFragment.newInstance(petId).
I want a separate ViewModel instance for each ...
6
votes
1
answer
1k
views
Is there a way to use Android's LifecycleObserver to notify about onActivityResult?
I currently use LifecycleObserver for notifying about events such as onStart of my view. This is great for components as presenters or handlers, such as FacebookLoginHandler that needs to register the ...
2
votes
1
answer
1k
views
Should my mvvm, livedata app cancel network requests when app is backgrounded?
Using Google's mvvm architecture components Github browser sample as a reference, how would one cancel a viewmodel hosted, live data observed, retrofit network request? :)
Maybe I'm reading it wrong, ...
0
votes
1
answer
2k
views
Could not resolve all files for configuration
I just implemented the Android Architecture Library in my Android project, everything works fine when i build the app gradle module, but i can't test it on a physical/emulator device this shows up on ...
8
votes
3
answers
6k
views
AndroidViewModel - Making duplicate calls doesn't return data in observe function
My question is related to ViewModel second time returns null wherein I am not getting callback inobserve function if I make a repeated call to server. Following is the code I am using -
@Singleton
...
7
votes
2
answers
4k
views
Android LiveData - switchMap is not triggered on second update
I have a LiveData object that depends on another LiveData. As I understand, Transformations.switchMap should allow to chain them. But switchMap handler is triggered only once and it doesn't react on ...
23
votes
7
answers
12k
views
How can I perform LiveData transformations on a background thread?
I have a need to transform one type of data, returned by a LiveData object, into another form on a background thread to prevent UI lag.
In my specific case, I have:
MyDBRow objects (POJOs consisting ...
2
votes
2
answers
7k
views
Failed to resolve: android.arch.persistence.room:runtime: 1.0.0-alpha1
I am unable to compile the following gradle :-
compile 'android.arch.persistence.room:runtime: 1.0.0-alpha1'
Error Message :-
Error:(26, 13) Failed to resolve: android.arch.persistence.room:...
0
votes
1
answer
403
views
Custom semi-generalized Factory for ViewModels
I'm trying to create a generalized ViewModelProvider.Factory to instantiate my ViewModels
class BundledAppFactory(private var bundle: AppParameterBundle) : ViewModelProvider.NewInstanceFactory() {
...
0
votes
1
answer
161
views
Please explain android architecture when using "First offline aproach".
my app architecture, quite common:
Please explain me if I have list of Entities, for example
@Entity(tableName = TABLE_NAME)
class Item constructor(
@PrimaryKey(autoGenerate = false) ...
4
votes
1
answer
215
views
Android Architecture Components with Data Binnding
In Android Architecture Components docs on the Handling Lifecycle page in Best practices paragraph, written:
Use Data Binding to maintain a clean interface between your views and the UI controller.
...
16
votes
2
answers
2k
views
Pre-launch-report failures due to missing methods (in com.google.android.apps.mtaas.crawler-1/base.apk)
Since recently my app started to contain strange error messages in the pre-launch reports (automatically generated after upload to the Play store).
These reports contain exceptions such as the ...
0
votes
1
answer
244
views
Fragment lifecycle on visibility changed
In my app I have two fragments and one of them (FragmentSlidingUp) can slide up and cover the other one (FragmentContent) completely. I use this library for the sliding up https://github.com/umano/...
2
votes
1
answer
2k
views
Strange LiveData behavior vs ObservableField
I have a problem with LiveData from new Android Architecture Components. I have used ObservableField before but wanted to try ACC.
When I set up value by MutableLiveData.setValue 4 times in one ...