Instructors can use these suggestions as little or as much as they want, and should feel free to assign any other homework they feel is appropriate.
If you're working through this codelab on your own, feel free to use these homework assignments to test your knowledge. To avoid losing data during a device-configuration change, you should save app data in which class?
A ViewModel should never contain any references to fragments, activities, or views. True or false? Start the next lesson: LiveData and LiveData observers. For links to other codelabs in this course, see the Android Kotlin Fundamentals codelabs landing page. Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4. For details, see the Google Developers Site Policies.
Welcome This codelab is part of the Android Kotlin Fundamentals course. The ViewModel class allows data to survive device-configuration changes such as screen rotations and changes to keyboard availability. You use the ViewModelFactory class to instantiate and return the ViewModel object that survives configuration changes. What you should already know How to create basic Android apps in Kotlin.
How to use the navigation graph to implement navigation in your app. How to add code to navigate between your app's destinations and pass data between navigation destinations. How the activity and fragment lifecycles work.
How to add logging information to an app and read logs using Logcat in Android Studio. What you'll learn How to use the recommended Android app architecture. How to retain UI data through device-configuration changes.
What the factory method design pattern is and how to use it. What you'll do Add a ViewModel to the app, to save app's data so the data survives configuration changes. Use ViewModelFactory and the factory-method design pattern to instantiate a ViewModel object with constructor parameters.
App overview In the Lesson 5 codelabs, you develop the GuessTheWord app, beginning with starter code. The first player acts out the word, being careful not to actually say the word itself.
When the second player guesses the word correctly, the first player presses the Got It button, which increases the count by one and shows the next word. If the second player can't guess the word, the first player presses the Skip button, which decreases the count by one and skips to the next word. To end the game, press the End Game button. This functionality isn't in the starter code for the first codelab in the series.
Task: Explore the starter code In this task, you download and run the starter app and examine the code. Run the app on an Android-powered device, or on an emulator.
Tap the buttons. Notice that the Skip button displays the next word and decreases the score by one, and the Got It button shows the next word and increases the score by one. The End Game button is not implemented, so nothing happens when you tap it. Step 2: Do a code walkthrough In Android Studio, explore the code to get a feel for how the app works. Make sure to look at the files described below, which are particularly important. UI fragments The starter code has three fragments in three different packages under the com.
The wordList defined inside the resetList method is a sample list of words to be used in the game. The onSkip method is the click handler for the Skip button. It decreases the score by 1, then displays the next word using the nextWord method. The onCorrect method is the click handler for the Got It button. This method is implemented similarly to the onSkip method. The only difference is that this method adds 1 to the score instead of subtracting. From the game fragment, the user can navigate to the score fragment.
From the score fragment, the user can navigate back to the game fragment. Task: Find problems in the starter app In this task, you find issues with the GuessTheWord starter app. Run the starter code and play the game through a few words, tapping either Skip or Got It after each word.
The game screen now shows a word and the current score. Change the screen orientation by rotating the device or emulator. Notice that the current score is lost. Run the game through a few more words. When the game screen is displayed with some score, close and re-open the app.
Notice that the game restarts from the beginning, because the app state is not saved. Play the game through a few words, then tap the End Game button. Notice that nothing happens. Issues in the app: The starter app doesn't save and restore the app state during configuration changes, such as when the device orientation changes, or when the app shuts down and restarts. You could resolve this issue using the onSaveInstanceState callback.
However, using the onSaveInstanceState method requires you to write extra code to save the state in a bundle, and to implement the logic to retrieve that state. Also, the amount of data that can be stored is minimal. The game screen does not navigate to the score screen when the user taps the End Game button.
App architecture App architecture is a way of designing your apps' classes, and the relationships between them, such that the code is organized, performs well in particular scenarios, and is easy to work with. Inside the dependencies block, add the Gradle dependency for the ViewModel.
To help you better understand how the ViewModel is lifecycle-aware, add an init block with a log statement. In the GameViewModel class, override the onCleared method. Add a log statement inside onCleared to track the GameViewModel lifecycle.
In the GameFragment class, add a field of the type GameViewModel at the top level as a class variable. ViewModelProvider creates a ViewModel instance in association with the given scope an activity or a fragment.
The created ViewModel is retained as long as the scope is alive. For example, if the scope is a fragment, the ViewModel is retained until the fragment is detached.
Put this code inside onCreateView , after the definition of the binding variable. Use the ViewModelProvider. In Android Studio, open the Logcat pane and filter on Game. Tap the Play button on your device or emulator. The game screen opens. Enable the auto-rotate setting on your device or emulator and change the screen orientation a few times. But the GameViewModel is created only once, and it is not re-created or destroyed for each call. Provides recent live matches. Provides Live scorecard and Commentary.
Provides Score update specific to a team selected. Provide upcomming cricket series. Full Specifications. What's new in version 1. Release November 13, Date Added November 13, Version 1.
Operating Systems. Operating Systems Android. Additional Requirements None. Total Downloads 6, Downloads Last Week Report Software. Related Apps. Netflix Free. This puts the user in total control of what they receive. Want to go deeper? Fans just have to tap a matchup and be taken to an in-depth box score featuring deep data, scoring plays, rich graphics, betting odds, historical stats, and key players - plus much more.
You can also play topically-themed HTML games right from the app, each originally created by theScore. This is where all the breaking news lives, including original content from theScore and the best-of-the-rest from other team-focused outlets. Skip to content. NFL Football. NBA Basketball. NHL Hockey. MLB Baseball. EPL Soccer.
0コメント