kandi X-RAY | codelabs Summary
kandi X-RAY | codelabs Summary
codelabs
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Function to read the codelines from a directory
- Creates an action with the given data
codelabs Key Features
codelabs Examples and Code Snippets
Community Discussions
Trending Discussions on codelabs
QUESTION
This comes from near the end of the codelab found here:
Intro to debugging - Debugging example: accessing a value that doesn't exist
This is all inside the MainActivity.kt file
Here's my onCreate
...ANSWER
Answered 2022-Jan-10 at 16:51I honestly have no idea what that Codelab is doing, based off the code they provide. The app isn't going to render anything (not the layout, not any changes to the layout) before onCreate
finishes, and onCreate
won't finish until it's run all its code, including that repeat
block in the division
function it calls.
division
isn't starting any worker threads, so all Thread.sleep
is doing is blocking the main thread - it's hanging the app. And you're right, sleep
does take a millis value, not seconds - I get the feeling they didn't actually run this code, it's full of other mistakes and inconsistencies that honestly made it hard to work out what you were meant to be doing. Change which Log.d
call? The ones in onCreate? (They actually mean the Log.v
call in division
, I assume)
Here's how you'd use a thread in Kotlin - you need to create a new one (so you're off the main thread, so it can actually finish creating the activity and run the UI):
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
I am trying to show native ads in Flutter.
https://codelabs.developers.google.com/codelabs/admob-inline-ads-in-flutter
https://github.com/googlecodelabs/admob-inline-ads-in-flutter
I used this codelab but they are showing small native ads.
In fact, I successfully implemented their codelab in my Flutter project.
But I want to make size medium, not small.
https://developers.google.com/admob/ios/native/templates
GADTSmallTemplateView(It seems this one, I don't want like small size)
GADTMediumTemplateView(My aim is to make my native ads like this one)
What is height in the codelab?
...ANSWER
Answered 2022-Mar-08 at 16:21I summed height of all elements in the design. It was 308. Then, I think 310 will be an ideal number. No problem, when I make it 310. Everything seems good.
QUESTION
As far as I understood, each part of code which is related to any state will be changed(recomposed) with state changes →
And each state is an observable and the dependent UI part observes that state and is subscribed to it(the state) and whenever the state changes it will be notified(redrawing that part of UI) and happening recomposition.
But here in this article of google Thinking in compose, it says that recomposition happens with changing input, so I am confused.
Recomposition is the process of calling your composable functions again when inputs change. This happens when the function's inputs change. When Compose recomposes based on new inputs, it only calls the functions or lambdas that might have changed, and skips the rest. By skipping all functions or lambdas that don't have changed parameters, Compose can recompose efficiently.
Also, here in other sample of compose-roadmap, encourage hoisting the state to make the composable function stateless to avoid recompositions
So it would be great, if someone can make it clear that the recompositions happens only with state changes or input changes also causes?
Thank you in advance for your help
...ANSWER
Answered 2022-Feb-17 at 11:34Recomposition happens when either the parameter values change or a mutable state variable within the composable function is updated.
A stateless function, sometimes referred to as an "immutable" function, is one where no data is stored in the function. This means that if you call the function repeatedly, it would be the same as if you had called it for the first time. The function retains no memory of any variables each time it is called.
Hoisting the state of a composable means that the state variables are kept outside of the composable and the values of those state variables are passed into the composable as normal parameters. This allows you to reuse the composable. This is especially important if you are creating a library of composables that you want to reuse from one project to another. Users of the library can be assured that there are no state dependencies that the composable requires. If you want a truely stateless composable, you would avoid doing things like passing in viewmodels. A viewmodel is very much dependent on the app in which it resides and will have code in it that is very specific to the app. A stateless composable has no dependencies on the app and therefore can be reused elsewhere.
That doesn't mean that you can't use viewmodels in your composables. Initially Google was against this when Compose first came out but realized that it was very awkward for developers. If a developer had no reason to make a reusable composable outside of the screen that it appears on, hoisting the state for every composable becomes a pain resulting in unnecessary boilerplate code. So the general rule is that if your composable is not going to be reused elsewhere and you need access to viewmodel data, you can either pass in the viewmodel as a parameter or access it within the composable through other means.
Aside from viewmodels, you may still want to make a composable stateful even when it needs to be reused. A good example of this is if you are using a TextField. As you are typing in text, you want the text to appear. Without using a state variable to retain the typed characters, you won't see the TextField update. For this reason, using a local state variable to store the entered characters is acceptable. While this doesn't make the composable stateless, it is still something you need to implement. However, even in this example, you can still make it stateless by passing the typed characters back up to the hoisted function and storing it in a viewmodel state variable that in turn triggers the hoisted function to recompose the composable and pass in the text that the TextField needs. But this is rather convoluted and a big round trip just to get characters shown in the TextField and therefore isn't recommended. You might want to do it though if you have a very complex TextField composable and you need to process the characters in your viewmodel as they are being typed - such as might be the case for doing a suggestion lookup for a url.
So even if your composable is stateless but your viewmodel has a mutable state variable that the hoisted function is observing, if the mutable state of that variable changes, the hoisted function will recompose. But whether the composable that the hoisted composable is calling gets recomposed depends on whether the parameter values to that composable change. If they change, a recomposition will occur.
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
I am trying to follow the "Adding Google Maps to a Flutter app" tutorial from link: https://codelabs.developers.google.com/codelabs/google-maps-in-flutter#3
After running the code in Android Studio (on MAC) I get the following:
...ANSWER
Answered 2021-Dec-30 at 10:43Try this basic Map code, hope you understand and if you have any doubts please feel free to ask in the comments section.
QUESTION
This android tutorial has the below code snippet:
...ANSWER
Answered 2022-Jan-05 at 21:48Similarly to for example suspend
functions, @Composable
functions are processed by the compiler in a very special way. This is to allow automatic recompositions and also to implicitly pass the context between components.
Documentation for @Composable specifies:
A useful mental model for Composable functions is that an implicit "composable context" is passed into a Composable function, and is done so implicitly when it is called from within another Composable function. This "context" can be used to store information from previous executions of the function that happened at the same logical point of the tree.
We can see it in action by writing a simple composable function and analyzing the resulting bytecode. Taking this source code:
QUESTION
The Code A is from the offical sample project.
I don't understand what val tasks = remember { mutableStateListOf(*allTasks) }
mean, could you tell me ?
BTW, Android Studio give me some information, you can see Image A
Code A
...ANSWER
Answered 2021-Dec-06 at 08:48From the documentation of varargs:
When you call a vararg -function, you can pass arguments individually, for example asList(1, 2, 3). If you already have an array and want to pass its contents to the function, use the spread operator (prefix the array with *):
QUESTION
I am a total beginner with kotlin and android development. I followed Persist data with Room tutorial and I have a popular problem to save my data:
...ANSWER
Answered 2021-Nov-19 at 23:55Room DAO functions should always suspend so it can't block the main UI thread, you can refer this google recommended example: https://developer.android.com/codelabs/android-room-with-a-view-kotlin#5
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install 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