android-compose-codelabs | repository contains a set of Android Studio projects | Android library
kandi X-RAY | android-compose-codelabs Summary
kandi X-RAY | android-compose-codelabs Summary
This repository contains a set of Android Studio projects to help you learn about Compose in Android. Each sample contains the code for a specific Compose codelab. For more information about Jetpack Compose, please read the documentation.
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 android-compose-codelabs
android-compose-codelabs Key Features
android-compose-codelabs Examples and Code Snippets
Community Discussions
Trending Discussions on android-compose-codelabs
QUESTION
The Code A is from the official sample project.
I don't think the produceState
is necessary, so I think I can replace Code A with Code B, is it right?
BTW, the Code B can run.
Code A
...ANSWER
Answered 2022-Mar-20 at 06:31Let's take a look at productState
under the hood:
QUESTION
The Code A is from the official sample project.
I'm learning the Compose by the article and the project.
I was told by the article the following content.
We could map what the screen needs to display and the UiState in the ViewModel layer by using a stream of data, a StateFlow of type DetailsUiState, that the ViewModel updates when the information is ready and that Compose collects with the collectAsState() API that you already know about.
Could you change Code A as StateFlow in ViewModel to do the same work?
Code A
...ANSWER
Answered 2022-Feb-23 at 19:45Hey here is my answer to your question
QUESTION
The following Code A are from the official sample project.
The paramter Modifier
is passed among the functions again and again in Code A.
I don't understand fully why the author need to design to pass the paramter Modifier
among the functions again and again .
I think Code B is simple. What benefits will the design framework of Code A get ?
Code A
...ANSWER
Answered 2022-Feb-22 at 15:30statusBarsPadding()
or navigationBarsPadding()
as a matter of fact any other Modifiers has to added in its relevant functions.
In the above Code B, let's say DetailsActivity
has BottomNavigationBar
.
Now BottomNavigationBar padding has to be added to DetailsScreen
.
It is not always possible to add all the Modifiers to DetailsContent
(from the above code) when we have more composable functions or in future, that can be changed to some other.
Maintenance will be easy when we have modifiers at every level.
QUESTION
I have read the article A safer way to collect flows from Android UIs.
I know the following content.
A cold flow backed by a channel or using operators with buffers such as buffer, conflate, flowOn, or shareIn is not safe to collect with some of the existing APIs such as CoroutineScope.launch, Flow.launchIn, or LifecycleCoroutineScope.launchWhenX, unless you manually cancel the Job that started the coroutine when the activity goes to the background. These APIs will keep the underlying flow producer active while emitting items into the buffer in the background, and thus wasting resources.
The Code A is from the official sample project.
The viewModel.suggestedDestinations
is a MutableStateFlo
w, it's a hot Flow.
I don't know if the operation collectAsState()
of hot Flow is safe in @Composable UI.
1: Do I need to use the Code just like Code B or Code C replace Code A for a hot Flow?
2: Is the operation collectAsState()
of cold Flow safe in @Composable UI.
Code A
...ANSWER
Answered 2022-Mar-12 at 10:51collectAsState
(Code A) is safe for any kind of Flow (cold/hot it doesn't matter). If you look at how collectAsState
is implemented then you will see that it uses a LaunchedEffect
deep down (collectAsState
-> produceState
-> LaunchedEffect
)
QUESTION
The following Code A is from the official sample project.
I think Code A is wrong, I think there are two Images in the line, the left image is a real image which is displayed when it has been loaded, and the right image is a temp image which is displayed when the real iamge is loading.
So I replace Code A with Code B.
But in fact the UI displays a temp image named ic_crane_logo first, then displays real image as I expected when I run Code A. And the UI keeps to display the temp iamge when I run Code B.
What is the problem with my thinking and code?
Code A
...ANSWER
Answered 2022-Mar-07 at 05:48rememberImagePainter
does not start loading an image.
It only starts when the corresponding Image
is added to the view tree and has non-zero dimensions.
As long as it is not loaded, Image
is a transparent view, and you get no performance gain by removing it from the view tree, so part Code A is completely correct.
QUESTION
The following Code A is from the official sample project.
The project use Hilt as Dependency Injection.
In my mind, I needn't create the ViewModel
object by myself because Hilt will create it automatically.
But in Code A, it seems that viewModel: MainViewModel = viewModel()
must be created manually, why?
Code A
...ANSWER
Answered 2022-Feb-27 at 03:50By creating manually, I mean calling the view model constructor. And this is not practiced with Hilt, because you have to pass down all the injections.
You can't just declare the viewModel: MainViewModel
parameter, because then you have to pass it from the calling view.
The viewModel()
does all the injection magic for you. Also, if the view model already exists, the same object will be returned. So it's completely automatic.
QUESTION
The following Code A is from the official sample project.
It seems that I get the same UI result after I replace the two fillParentMaxWidth()
with fillMaxWidth()
.
What are differents between fillParentMaxWidth()
and fillMaxWidth()
in Compose?
Code A
...ANSWER
Answered 2022-Feb-25 at 14:27As here LazyColumn
is used fillParentMaxWidth
fill be same as fillMaxWidth
.
In case of LazyRow
, fillMaxWidth
will raise an error. So fillParentMaxWidth
can be used.
And the other way for LazyColumn
.
QUESTION
The following code are from the official sample project.
There are two branches, main and end.
I found the Code main and the Code end using different ways to navigate.
Code main is simple and clear, and in other projects, it navigate based State just like Code A which is from the project.
Code end use NavHostController
to navigate, but It seems that we need't to use Navigation
again when we use Jetpack Compose, right?
Code main
...ANSWER
Answered 2022-Jan-04 at 08:22I've worked with Compose since the early alpha stages and became quickly disappointed with Google's lame attempt at providing a more modern approach to navigating a single-activity app. When you consider that Android's view-based system was entirely replaced with the declaration approach that Compose uses, you have to seriously wonder why they would stick with a navigation controller that doesn't allow you pass objects from one screen to another. There was also the issue that adding animation when transitioning from one screen to another was an afterthought. There is an add-on that supports animation transitions.
But perhaps the worst thing about Compose was its lack of handling device configuration changes. Under the older view-based system, you defined your layouts in xml files and placed these in resource folders that had qualifiers in the folder name that would aid Android in picking the correct layout based on things like screen density, orientation, screen size, etc. That went out the window with Compose. Eventually Google did add APIs to handle composables that need to be selected based on screen sizes and densities. But ultimately, you end up writing this decision logic within your composable and your code starts to look like spaghetti. Google's Android team completely forgot about the most basic "Separation of Concerns" when they chose to mix UI layouts with the logic that determines which layout gets selected. Designing your composables is one thing and how they get selected is another. Don't mix the two. Your composables become increasingly less reusable when you integrate those APIs. The original developers of Android (who weren't from Google) knew well enough to have the operating system manage the layout selection based upon changes to device configurations.
I chose to create my own framework that handles navigation and manages composables in almost an identical way that the view-based system works. It also remedies the issue of not being able to pass objects from screen to screen. If you are interested, you can check it out at:
https://github.com/JohannBlake/Jetmagic
So to answer you question about whether Compose Navigation is good for navigating, I would have to say no. I have worked with it and have found it severely lacking. If you want to be just-another-run-of-the-mill-Android-developer, then use Compose Navigation. But if you want to chart your own path and liberate yourself from Google's poor design practices, then use Jetmagic or even better, create your own navigation framework. It isn't that hard.
QUESTION
The Code A is from the main branch of the official sample project.
There are three subclass Overview
, Accounts
and Bills
of the enum class RallyScreen
in the project.
There is a function fun content(onScreenChange: (String) -> Unit) { body(onScreenChange) }
which accept the paramater onScreenChange : (String) -> Unit
in the class RallyScreen
.
Although the statement is enum class RallyScreen(val body: @Composable ((String) -> Unit) -> Unit) {..}
, the body = { OverviewBody() }
in the class Overview
, the body = { AccountsBody(UserData.accounts) }
in the class Accounts
and the body = { BillsBody(UserData.bills) }
in the class Bills
don't require to pass the paramter (String) -> Unit)
, why can Kotlin run well and navigate by Tab well when the App launch fun content(onScreenChange: (String) -> Unit) { body(onScreenChange)}
?
Code A
...ANSWER
Answered 2022-Jan-09 at 06:26Remember that each of the on
variables is a callback. onScreenChange
implicitly passes the variable it
as String, but it is not necessary to use the variable within your callback.
it
is implicitly passed, but in the GitHub link you gave, the programmer decided to renameit
toonScreenChange
. That callback then was passed toOverviewBody
, which overrides the default{}
, as you said. What I don't understand about your question is whereonAccountClick
is coming from. That is not a parameter ofOverviewBody
, according to the GitHub code.content
has a parameter for another callback, which you noticed. Thebody
variable for each class in the enum is then invoked, and the callback parameter is passed to the invocation. In the future, that callback will only be used byOverviewBody
. Remember thatbody(callback)
is the same asbody.invoke(callback)
.
I hope this helps. Please let me know if anything needs some more explanation.
For further reference:
Additional Content Edit:
- I see the parameters in your code, not the one on GitHub. My apologies for missing that.
body
is a variable function. This means that the variable function can be invoked, either by writingbody(...)
orbody.invoke(...)
. The String parameterit
, which is passed implicitly, is ignored and the default values are used, unless specified like you suggested (onAccountClick=it
).
QUESTION
The Code A is from the end branch of the official sample project.
The project use Hilt to implement dependency injection.
In my mind, I needn't to instance a ViewModel
class manually when I use Hilt, and the system will automatically instance a ViewModel
when it needs.
But It seems that the author use the code viewModel: MainViewModel = viewModel()
to instance the ViewModel
class manually in Code A, how can I instance a ViewMode
class automatically with Hilt when I use Compose?
Code A
...ANSWER
Answered 2022-Jan-01 at 18:49As stated in the documentation
The viewModel() function mentioned in the ViewModel section automatically uses the ViewModel that Hilt constructs with the @HiltViewModel annotation. We've provided documentation with information about Hilt's ViewModel integration.
So as long as your viewmodel has the @HiltViewModel annotation, you don’t have to change anything.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install android-compose-codelabs
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