compose-samples | Official Jetpack Compose samples | Android library
kandi X-RAY | compose-samples Summary
kandi X-RAY | compose-samples Summary
This repository contains a set of individual Android Studio projects to help you learn about Compose in Android. Each sample demonstrates different use cases, complexity levels and APIs. For more information, 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 compose-samples
compose-samples Key Features
compose-samples Examples and Code Snippets
Community Discussions
Trending Discussions on compose-samples
QUESTION
I'm trying to get a simple map as my mainActivity to test how maps with compose works. Unfortunately I just fail at the beginning with an Errormessage which says basically nothing but "IllegalStateException". I've tried to extract the map code from the original Google example here: https://github.com/android/compose-samples/tree/main/Crane I've tried to rebuild a simple Composable, made an API Key at Google Cloud Platform and added it to my manifest.
Thats the MainActivity
:
ANSWER
Answered 2021-Nov-14 at 18:43I don't know if you are reading this, but you have forgotten to include ON_START event for lifecycle, so you are throwing the exception. Also you are calling onStart on ON_CREATE.
Also consider populating the lifecycleObserver to be scoped into the DisposableEffect and make the DisposableEffect also aware of the mapView (give it as an argument).
QUESTION
I'm probably missing something obvious but I'm unable to show the Deploy Preview button in my Android Studio.
However if I open the Android Compose Samples I see them.
I checked their dependencies and applied them to my app, but I'm still not able to see them.
...ANSWER
Answered 2022-Feb-04 at 11:21Did you enable it on the experimental panel in AS settings?
Also, I believe you're missing the preview dependency
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
QUESTION
I was looking at the JetSurvey project (Android Jetpack Compose sample project) and noticed that they created a class to wrap the LiveData value in their ViewModel class. Here is the class I'm talking about:
...ANSWER
Answered 2022-Jan-20 at 17:26Suppose your LiveData observer in a Fragment navigates to a second Fragment. The user rotates the screen so the first Fragment instance is destroyed. When they back out of the second Fragment, a new instance of the first Fragment is recreated, so its observer is triggered again. Without the event wrapper, it would suddenly and surprisingly navigate back to the second Fragment immediately.
Also, a LiveData might have multiple observers. Maybe two different Fragments are observing the same events LiveData, but you don't want to risk showing the user a message twice for the same event. For example, they could navigate to a second Fragment that is observing the same events LiveData as the first Fragment. An event fires and the second Fragment observer shows the user a dialog box or something. Then the user backs up to the first Fragment and that same event will fire the first Fragment's observer so the event gets handled twice.
If your project uses coroutines, a cleaner solution for this event observing problem is to use a SharedFlow with replay
of 0 instead of using a LiveData with an event wrapper class. I suspect the reason they didn't use SharedFlow in the JetSurvey project is that they don't want to assume you're already familiar with Flows when that's not what the example is about.
An alternate solution called SingleLiveEvent appeared in an official Android example once, but was considered too hacky to add to the Jetpack libraries.
QUESTION
I have a composable containing a google maps view. When I click on a pin on the map I would like to trigger navController.navigate
so I can navigate to another composable. However, when I call it the application gets stuck instead of navigating. Navigating on a button clicks works as expected.
I have also created a very simple application that is demonstrating the problem. The MainActivity
looks like this:
ANSWER
Answered 2021-Dec-08 at 19:38After some debugging, I come to the conclusion that the problem is somehow related to Lifecycle.Event.ON_STOP -> mapView.onStop()
getMapLifecycleObserver
Removing it solves the problem.
I'm not happy with the solution and would be happy if someone is able to provide a better solution, or at least an answer explaining more details about the problem.
QUESTION
I have started to learn Jetpack compose. I was checking this sample from Google : https://github.com/android/compose-samples/blob/main/Crane/app/src/main/java/androidx/compose/samples/crane/home/MainActivity.kt
As you see MainScreen composable function is a method in the file and not an inner function in MainActivity class. MainScreen has not been used anywhere else. So why not define it as a private inner function in the Activity?
Would you please describe me the reason of that?
...ANSWER
Answered 2021-Dec-09 at 10:05They only need to be defined outside of a class if you plan on using the @Preview annotation, which allows Android Studio to render the composable in a preview pane. They should also be defined outside of the class if you plan on reusing the composable elsewhere in your app or make them generally reusable for other apps.
You can however define it as a inner function if you want to but you can't use the @Preview annotation. Nevertheless, if you don't plan on using preview and you have no reason to reuse the composable outside of the class, you can define it as an inner function. I've done that on many composables.
QUESTION
I am new to JetPack Compose. I am leanring Compose
and was following https://developer.android.com/jetpack/compose/tutorial
But when I ran the application in my android device, the app was extremely slow.
Simple things like expanding long message and changing color of message is taking so much time.
I have googled the problem and found:
- https://www.reddit.com/r/androiddev/comments/oatiur/why_simple_app_with_jetpack_compose_is_5x_times/
- https://github.com/android/compose-samples/issues/21
- https://jetc.dev/slack/2021-03-14-why-so-slow.html
- Jetpack Compose Performance Issue that only occurs in multi module project
But none of these were helpful to me.
If there is any confusion, please tell in the comments.
...ANSWER
Answered 2021-Nov-27 at 04:37I experienced the same thing. What is currently happening is:-
1.) App Startup is kinda slow
2.) App is glitchy upon start
The good news is:-
1.) The glitches only happen for the first few times you run the animation,
2.) The glitches seem to disappear from every element after you break one of the elements in.
The bad news is of course, you have to repeat the above steps upon EVERY startup. I have heard that running the production builds instead of the debug variants boosts up the performance significantly. As of now, there's nothing you can do about it. It will only break in as Compose develops. You can check the official Compose Samples too. They are as glitchy as your apps.
All you can really do at this point is wait.
EDITS BASED ON THE COMMENTS BELOW:-
1.) By The glitches seem to disappear from every element after you break one of the elements in., I mean that if you have a lot of animated content, like maybe two LazyColumn
s, and a few others, then upon swiping the Column back and forth a few times (breaking it in), the lag will be gone from the other columns, as well as the animated content. Element
there meant element of the screen, so individual LazyColumn
s are each elements.
2.) To get the production build, all you need to do is click on the release
tab in the left edge of the screen in studio, then select release
from the dropdown list instead of debug
. 'Production' was substituted by me for 'release', but it's one and the same thing you see.
QUESTION
The Code A is from a Android offical sample project here.
The author create a val uiState
, it's MutableStateFlow
, I know that MutableStateFlow
is hot flow, it will occupy system resource when it is created.
Do I need to destory a StateFlow
object created in view model by myself? will the system release it automatically when the app doesn't need it again?
Code A
...ANSWER
Answered 2021-Oct-29 at 06:49It's a normal object and as such will be cleaned up once there are no references to it (so when both observing view and ViewModel seizes to exist)
QUESTION
The Code A is based a Android offical sample project here.
The Code A can display correct data in UI.
If I use Code B, I find that nothing is displayed.
It seems that _uiState=_uiState.copy(...)
doesn't make uiState
to notice UI that the data has changed in Code B.
What is wrong with Code B?
Code A
...ANSWER
Answered 2021-Oct-27 at 06:52I don't see your pattern described in the official docs:
https://developer.android.com/jetpack/compose/state
It is possible it worked under an older version of Compose and doesn't work under the current version??
According to the docs, recomposition can only occur when you use mutableStateOf
in conjunction with a remember
and set the value property to a new value to trigger the recomposition:
QUESTION
The Code A is from offical sample project here.
The InterestsViewModel
define uiState
as StateFlow
, and it is converted as State
by collectAsState()
in the Composable function rememberTabContent
.
I'm very strange why the author doesn't define uiState
as State
directly in InterestsViewModel
, so I write Code B.
The Code B can be compiled , and it can run, but it display nothing in screen, what is wrong with Code B ?
Code A
...ANSWER
Answered 2021-Oct-27 at 03:20The uiState
that you are using in your Composable val uiState: InterestsUiState = _uiState
is not a State and hence doesn't respond to changes. It's just a normal InterestsUiState
initialized with the current value of _uiState
.
To make it work, you can simply expose the getter for _uiState
.
QUESTION
The Code A is from offical sample code here.
val (currentSection, updateSection) = rememberSaveable { mutableStateOf(tabContent.first().section) }
is a destructuring declaration.
It's a clear that currentSection
is assigned by tabContent.first().section
What is assigned to the variable updateSection
? Will it be assigned by tabContent.first().content
?
Code A
...ANSWER
Answered 2021-Oct-26 at 04:25updateSection
is a lambda here used to update the value of the mutable state.
Consider this example:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install compose-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