architecture-components-samples | Samples for Android Architecture Components | Architecture library
kandi X-RAY | architecture-components-samples Summary
kandi X-RAY | architecture-components-samples Summary
Android Architecture Components samples.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of architecture-components-samples
architecture-components-samples Key Features
architecture-components-samples Examples and Code Snippets
Community Discussions
Trending Discussions on architecture-components-samples
QUESTION
In View system there are official examples how to implement loading states and adding header and footer item to the list:
https://developer.android.com/topic/libraries/architecture/paging/load-state
I didn't really found anything similar for Jetpack Compose
Only how to show items
https://developer.android.com/jetpack/compose/lists#large-datasets
But how can we implement load states in Compose?
...ANSWER
Answered 2022-Mar-30 at 09:13We are doing something like this, it works well:
QUESTION
Using the newest Navigation Component with the BottomNavigationView
, the NavController
now saves and restores the states of tabs by default:
As part of this change, the NavigationUI methods of onNavDestinationSelected(), BottomNavigationView.setupWithNavController() and NavigationView.setupWithNavController() now automatically save and restore the state of popped destinations, enabling support for multiple back stacks without any code changes. When using Navigation with Fragments, this is the recommended way to integrate with multiple back stacks.
This is great! Now switching tabs gives you the last viewed stack.
But, if the user reselects a tab, say they've gone Home -> Detail Page A -> Detail Page B
, then they select the Home
tab expecting to go back to the default view, they still see Detail Page B
.
It seems like part of the discussion was to handle the "reselecting a tab" behavior as mentioned in the issue tracker, but I can't figure out the recommended way for implementing this.
All that's included in the NavigationAdvancedSample is:
...ANSWER
Answered 2021-Aug-14 at 00:04BottomNavigationView
has its own method for handling reselection via setOnItemReselectedListener()
(or, when using an earlier version of the Material Design Library, the now deprecated setOnNavigationItemReselectedListener()
).
bottomNavigationView.setupWithNavController
does not set this listener (as there is no Material specification for exactly what reselecting a tab should do), so you need to set it yourself:
QUESTION
The Problem
I have locally-generated data that I need to display in a RecyclerView
. I tried to use a custom PagingSource
with PagingDataAdapter
to reduce the amount of data in memory, but I get visual effects when I invalidate the data, for example if I insert or delete one item:
- when the visible items belong to 2 different "pages", many unchanged items flash as if they had been modified
- if all the visible items belong to the same page, everything is fine. Only the inserted / deleted item shows an animation.
I took an example application referenced by Google's doc (PagingSample) to test the concept. The original with Room does not show artefacts, but my modified version with custom PagingSource
does.
The code generated and used by Room is too complicated to see any difference that would explain the issue.
My data must be locally-generated, I can't use Room as a work-around to display them.
My question
How can I properly define a PagingSource
for my local data, and use it with PagingDataAdapter without visual glitches?
Optionally, how can I know when data is discarded (so I can discard my local data as well)?
Code excerpts and details
The full example project is hosted here: https://github.com/blueglyph/PagingSampleModified
Here is the data:
...ANSWER
Answered 2022-Jan-26 at 11:16I finally found out a possible solution, though I'm not sure it will work if the Paging-3 library is updated. I don't know yet if the behaviour explained above is due to bugs / limitations in the Paging-3 components or if that's just a bad explanation in the reference documentation, or something I missed entirely.
I. Work-around for the glitch issue.
In
PagingConfig
, we must haveenablePlaceholders = true
or it simply won't work correctly. Withfalse
I'm observing the scrollbar jumping on eachload
operation when scrolling up/down, and inserting items at the end will make all items glitch on the display, then the list will jump all the way to the top.The logic in
getRefreshKey
andload
, as shown in Google's guide and reference documentation, is naive and will not work with custom data. I had to modify them as follows (modifications have been pushed in the github example):
QUESTION
I've downloaded a repository from github and in the dependencies I've founded this
...ANSWER
Answered 2021-Dec-02 at 16:33This is a way (with ExtraPropertiesExtension) to keep build.gradle
files as clean as possible with an external file like a version.gradle
file in which there are all dependencies and their version.
QUESTION
I'm using a BottomNavigationView to switch between a Home and Meetings fragment. Each time I switch between them, each Fragment is recreated. I'm using a SharedViewModel between them for a shared "meetingID" variable, but that's being destroyed and recreated along with the Fragments (aggravatingly so). I'm using Jetpack Navigation Component; I almost completely copied the Google example here. It's still not working, however.
Here's a look at my onCreate and onSupportNavigateUp:
...ANSWER
Answered 2021-Nov-15 at 21:28@ianhanniballake was onto the right idea!
I was foolishly using the LoginFragment as my home destination to awkwardly do some conditional logic. A quick refactor, and with HomeFragment set as the actual home destination, it now works. Thanks, @ianhanniballake.
QUESTION
I would like to implement a navigation logic based in the Android Navigation Component in it's more updated version (2.4.0-beta02) but I'm facing some kind of issues (or non expected behaviors) related with the Android lifeycle.
My main problem is that each time you change a tab, fragments are totally recreated which means that onCreate() method it's called every time the tab is changed. I understand that for performance reasons, fragments call onDestroyView() method each time you left the tab associated with it and then when you came back to that tab onCreateView() method will be called. Actually I don't know if my implementation of the Navigation Component and it's behavior it's correct or not and I would like to know how correctly implement it.
My actual implementation looks like this:
Gradle file:
...ANSWER
Answered 2021-Nov-13 at 19:05The Navigation Component calls onDestroy() method each time a tab it's changed so it's the normal behaviour.
QUESTION
I want to implement my custom PagedList.BoundaryCallback with coroutines, liveData and repository pattern using Paging Library 2, but I couldn't find a good example of integration of these libraries.
Even in Android official samples, they use enqueue and callbacks to perform the API request with paging 2...
I read this Medium post also but it use a coroutines scope inside the boundary callback and I think that is not a good practice.
Is there any way to achieve it? Or should I migrate to Paging 3?
...ANSWER
Answered 2021-Sep-01 at 15:38This solution works for me:
I injected a CoroutinesDispatcherProvider in my Repository:
QUESTION
I want to UnitTest my AuthRepository
which is using this "flow-version" of NetworkBoundResource (find the code for that below)
My current UnitTest is green when writing it like this:
...ANSWER
Answered 2021-Aug-29 at 10:07While trying to implement further tests, the number of problems increased. In the end I did a rewrite of networkBoundResource
that turned out a lot better than the one circulating on SO. The according question and answer together with code can befound here:
Kotlin Flow: emitAll is never collected
Original AnswerThanks to @aSemy, I was forced to dig deeper. It turned out to be a series of errors I had. None of them very big, but annoying to find out. Here they are:
1. Breakpoints with coroutinesWhile debugging and setting breakpoints, you should make sure to set the thread suspension to "All". This can be easily done by right-clicking on a breakpoint, choose the radio button "All" and click "Make default". Every breakpoint from then on will have the right setting.
2. Catch flow exceptions correctlyAt some point I realized that I have the .catch
function setup, but that I do not get any exceptions or any helpful hint out of it. This is because I ignored the exception param coming into this function. Changing the code to
QUESTION
While working on my android app, I found this example on the android developer site. Specifically, I don't understand the interface UserDataSource ( in /observability) and its corresponding localUserDataSource implementation class (in /observability/persistence). This doesn't look like a repository to me, but it contains an instance to a DAO like a repository would.
- Does this approach/pattern have a name?
- Are there advantages and disadvantages to doing this over a repository? (It seems like its a lot of redundant code, but I don't know what I'm doing yet)
- Should I have a repository in addition to this?
I think I'm being confused by the different ways of doing things, and seeing a different approach to what I was expecting made me wonder what I really needed (or what is correct, more extensible, etc.). Thank you so much for any light you can shine on this for me.
...ANSWER
Answered 2021-Jun-04 at 20:42
- Does this approach/pattern have a name?
The project that you linked is structured using the recommended app architecture described in the official Android docs:
- Are there advantages and disadvantages to doing this over a repository? (It seems like its a lot of redundant code, but I don't know what I'm doing yet)
- Should I have a repository in addition to this?
In order to clarify, UserDataSource
is not a repository, it is a data source. In that project, UserDataSource is used by a repository: UserRepository. As per official docs:
You can consider repositories to be mediators between different data sources.
Therefore the recommended way is to have both a repository and data sources. The advantage is also described in the official docs:
Even though the repository module looks unnecessary, it serves an important purpose: it abstracts the data sources from the rest of the app. Now, our UserProfileViewModel doesn't know how the data is fetched, so we can provide the view model with data obtained from several different data-fetching implementations.
QUESTION
I am using Navigation component in my App, using google Advanced Sample(here). my problem is when going back to a fragment, the scrolling position does not lost but it rearranges items and moves highest visible items so that top of those item align to top of recyclerview. please see this:
before going to next fragment:
and after back to fragment:
this problem is matter because some times clicked item goes down and not seen until scroll down. how to prevent this behavior?
please consider:
this problem exist if using navigation component to change fragment. if start fragment using
supportFragmentManager.beginTransaction()
or start another activity and then go to this fragment it is OK. but if I navigate to another fragment using navigation component this problem is exist.(maybe because of recreating fragment)also this problem exist if using fragment in ViewPager. i.e recyclerView is in a fragment that handle with ViewPagerAdapter and viewPager is in HomeFragment that opened with Navigation component. if recyclerView is in HomeFragment there is no problem.
no problem with LinearLayoutManager. only with StaggeredGridLayoutManager.
there is not difference if using ViewPager2 and also FragmentStatePagerAdapter
I try to prevent recreate of fragment(by this solution) but not solved.
UPDATE: you can clone project with this problem from here
...ANSWER
Answered 2021-Jan-05 at 13:46The Navigation Component behavior is normal when you navigate from one fragment to another. I mean, onDestroyView()
method from the previous fragment is executed, so it means that your view is destroyed, but not the fragment. Remembering that fragment has two lifecycles one for the fragment and another one for the view, There was a video about it.
Also, there were issues registered in issue tracker in order to avoid this behavior in some cases and the GitHub issues:
- https://issuetracker.google.com/issues/127932815
- https://github.com/android/architecture-components-samples/issues/530
The problem is that when you have fragment that is heavy to recreate, is easier to do not destroy it and just add one fragment. So, when you go back it is not recreated. But, for this behavior is not part of navigation component.
Solutions
The easiest solution is to not use navigation component and work with the tradicional way, as you can see this works perfectly in you use case.
You can use the traditional way just for this use case, and use the navigation component for other cases.
You can inflate this view in an activity. So you are adding un activity
But if the previous tree options is not possible. You can try the following:
- If you are using viewModel, you can use SaveState. Basically, it can save the data from your fragment, it is like a map data structure, so you can save positions from your list or recycler view. When go back to this fragment, get the position from this saveState object and use
scrollToPosition
method in order to add the real position. - Recycler view have methods for restore positions. You can see the uses cases for that, because first you need the data and then add the real position, for more details you can visit this link. This configuration for recycler view is useful also when you lose memory and you need to recreate the recycler view with asynchronous data.
- If you are using viewModel, you can use SaveState. Basically, it can save the data from your fragment, it is like a map data structure, so you can save positions from your list or recycler view. When go back to this fragment, get the position from this saveState object and use
Finally, if you want to understand more about how fragment works with navigation component, you can see this link
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install architecture-components-samples
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page