Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Android Studio 4.2 Development Essentials - Java Edition

You're reading from  Android Studio 4.2 Development Essentials - Java Edition

Product type Book
Published in Aug 2021
Publisher Packt
ISBN-13 9781803238814
Pages 782 pages
Edition 1st Edition
Languages
Author (1):
Neil Smyth Neil Smyth
Profile icon Neil Smyth

Table of Contents (87) Chapters

1. Introduction 2. Setting up an Android Studio Development Environment 3. Creating an Example Android App in Android Studio 4. Creating an Android Virtual Device (AVD) in Android Studio 5. Using and Configuring the Android Studio AVD Emulator 6. A Tour of the Android Studio User Interface 7. Testing Android Studio Apps on a Physical Android Device 8. The Basics of the Android Studio Code Editor 9. An Overview of the Android Architecture 10. The Anatomy of an Android Application 11. An Overview of Android View Binding 12. Understanding Android Application and Activity Lifecycles 13. Handling Android Activity State Changes 14. Android Activity State Changes by Example 15. Saving and Restoring the State of an Android Activity 16. Understanding Android Views, View Groups and Layouts 17. A Guide to the Android Studio Layout Editor Tool 18. A Guide to the Android ConstraintLayout 19. A Guide to Using ConstraintLayout in Android Studio 20. Working with ConstraintLayout Chains and Ratios in Android Studio 21. An Android Studio Layout Editor ConstraintLayout Tutorial 22. Manual XML Layout Design in Android Studio 23. Managing Constraints using Constraint Sets 24. An Android ConstraintSet Tutorial 25. A Guide to using Apply Changes in Android Studio 26. An Overview and Example of Android Event Handling 27. Android Touch and Multi-touch Event Handling 28. Detecting Common Gestures Using the Android Gesture Detector Class 29. Implementing Custom Gesture and Pinch Recognition on Android 30. An Introduction to Android Fragments 31. Using Fragments in Android Studio - An Example 32. Modern Android App Architecture with Jetpack 33. An Android Jetpack ViewModel Tutorial 34. An Android Jetpack LiveData Tutorial 35. An Overview of Android Jetpack Data Binding 36. An Android Jetpack Data Binding Tutorial 37. An Android ViewModel Saved State Tutorial 38. Working with Android Lifecycle-Aware Components 39. An Android Jetpack Lifecycle Awareness Tutorial 40. An Overview of the Navigation Architecture Component 41. An Android Jetpack Navigation Component Tutorial 42. An Introduction to MotionLayout 43. An Android MotionLayout Editor Tutorial 44. A MotionLayout KeyCycle Tutorial 45. Working with the Floating Action Button and Snackbar 46. Creating a Tabbed Interface using the TabLayout Component 47. Working with the RecyclerView and CardView Widgets 48. An Android RecyclerView and CardView Tutorial 49. A Layout Editor Sample Data Tutorial 50. Working with the AppBar and Collapsing Toolbar Layouts 51. An Android Studio Primary/Detail Flow Tutorial 52. An Overview of Android Intents 53. Android Explicit Intents – A Worked Example 54. Android Implicit Intents – A Worked Example 55. Android Broadcast Intents and Broadcast Receivers 56. A Basic Overview of Java Threads, Handlers and Executors 57. An Overview of Android Services 58. Implementing an Android Started Service – A Worked Example 59. Android Local Bound Services – A Worked Example 60. Android Remote Bound Services – A Worked Example 61. An Android Notifications Tutorial 62. An Android Direct Reply Notification Tutorial 63. Foldable Devices and Multi-Window Support 64. An Overview of Android SQLite Databases 65. The Android Room Persistence Library 66. An Android TableLayout and TableRow Tutorial 67. An Android Room Database and Repository Tutorial 68. Accessing Cloud Storage using the Android Storage Access Framework 69. An Android Storage Access Framework Example 70. Video Playback on Android using the VideoView and MediaController Classes 71. Android Picture-in-Picture Mode 72. An Android Picture-in-Picture Tutorial 73. Making Runtime Permission Requests in Android 74. Android Audio Recording and Playback using MediaPlayer and MediaRecorder 75. Working with the Google Maps Android API in Android Studio 76. Printing with the Android Printing Framework 77. An Android HTML and Web Content Printing Example 78. A Guide to Android Custom Document Printing 79. An Introduction to Android App Links 80. An Android Studio App Links Tutorial 81. A Guide to the Android Studio Profiler 82. An Android Biometric Authentication Tutorial 83. Creating, Testing and Uploading an Android App Bundle 84. An Overview of Android Dynamic Feature Modules 85. An Android Studio Dynamic Feature Tutorial 86. An Overview of Gradle in Android Studio Index

25. A Guide to using Apply Changes in Android Studio

Now that some of the basic concepts of Android development using Android Studio have been covered, now is a good time to introduce the Android Studio Apply Changes feature. As all experienced developers know, every second spent waiting for an app to compile and run is time better spent writing and refining code.

25.1 Introducing Apply Changes

In early versions of Android Studio, each time a change to a project needed to be tested Android Studio would recompile the code, convert it to Dex format, generate the APK package file and install it on the device or emulator. Having performed these steps the app would finally be launched ready for testing. Even on a fast development system this is a process that takes a considerable amount of time to complete. It is not uncommon for it to take a minute or more for this process to complete for a large application.

Apply Changes, in contrast, allows many code and resource changes within a project to be reflected nearly instantaneously within the app while it is already running on a device or emulator session.

Consider, for the purposes of an example, an app being developed in Android Studio which has already been launched on a device or emulator. If changes are made to resource settings or the code within a method, Apply Changes will push the updated...

25.2 Understanding Apply Changes Options

Android Studio provides three options in terms of applying changes to a running app in the form of Run App, Apply Changes and Restart Activity and Apply Code Changes. These options can be summarized as follows:

Run App - Stops the currently running app and restarts it. If no changes have been made to the project since it was last launched, this option will simply restart the app. If, on the other hand, changes have been made to the project, Android Studio will rebuild and reinstall the app onto the device or emulator before launching it.

Apply Code Changes - This option can be used when the only changes made to a project involve modifications to the body of existing methods or when a new class or method has been added. When selected, the changes will be applied to the running app without the need to restart either the app or the currently running activity. This mode cannot, however, be used when changes have been made to...

25.3 Using Apply Changes

When a project has been loaded into Android Studio, but is not yet running on a device or emulator, it can be launched as usual using either the run (marked A in Figure 25-1) or debug (B) button located in the toolbar:

Figure 25-1

After the app has launched and is running, the icon on the run button will change to indicate that the app is running and the Apply Changes and Restart Activity and Apply Code Changes buttons will be enabled as indicated in Figure 25-2 below:

Figure 25-2

If the changes are unable to be applied when one of the Apply Changes buttons is selected, Android Studio will display a message indicating the failure together with an explanation. Figure 25-3, for example, shows the message displayed by Android Studio when the Apply Code Changes option is selected after a change has been made to a resource file:

Figure 25-3

In this situation, the solution is to use the Apply Changes and Restart Activity option (for...

25.4 Configuring Apply Changes Fallback Settings

The Apply Changes fallback settings are located in the Android Studio Preferences window which is displayed by selecting the File -> Settings menu option (Android Studio -> Preferences on macOS). Within the Preferences dialog, select the Build, Execution, Deployment entry in the left-hand panel followed by Deployment as shown in Figure 25-5:

Figure 25-5

Once the required options have been enabled, click on Apply followed by the OK button to commit the changes and dismiss the dialog. After these defaults have been enabled, Android Studio will automatically reinstall and restart the app when necessary.

25.5 An Apply Changes Tutorial

Launch Android Studio, select the Create New Project quick start option from the welcome screen and, within the resulting new project dialog, choose the Basic Activity template before clicking on the Next button.

Enter ApplyChanges into the Name field and specify com.ebookfrenzy.applychanges as the package name. Before clicking on the Finish button, change the Minimum API level setting to API 26: Android 8.0 (Oreo) and the Language menu to Java.

25.6 Using Apply Code Changes

Begin by clicking on the run button and selecting a suitable emulator or physical device as the run target. After clicking the run button, track the amount of time before the example app appears on the device or emulator.

Once running, click on the action button (the button displaying an envelope icon located in the lower right-hand corner of the screen). Note that a Snackbar instance appears displaying text which reads “Replace with your own action” as shown in Figure 25-6:

Figure 25-6

Once the app is running, the Apply Changes buttons should have been enabled indicating that certain project changes can be applied without having to reinstall and restart the app. To see this in action, edit the MainActivity.java file, locate the onCreate method and modify the action code so that a different message is displayed when the action button is selected:

binding.fab.setOnClickListener(new View.OnClickListener() {

  &...

25.7 Using Apply Changes and Restart Activity

Any resource change will require use of the Apply Changes and Restart Activity option. Within Android Studio select the app -> res -> layout -> fragment_first.xml layout file. With the Layout Editor tool in Design mode, select the default TextView component and change the text property in the attributes tool window to “Hello Android”.

Make sure that the fallback options outlined in “Configuring Apply Changes Fallback Settings” above are turned off before clicking on the Apply Code Changes button. Note that the request fails because this change involves project resources. Click on the Apply Changes and Restart Activity button and verify that the activity restarts and displays the new text on the TextView widget.

25.8 Using Run App

As previously described, the removal of a method requires the complete re-installation and restart of the running app. To experience this, edit the MainActivity.java file and add a new method after the onCreate method as follows:

public void demoMethod() {

}

Use the Apply Code Changes button and confirm that the changes are applied without the need to reinstall the app.

Next, delete the new method and verify that clicking on either of the two Apply Changes buttons will result in the request failing. The only way to run the app after such a change is to click on the Run App button.

25.9 Summary

Apply Changes is a feature of Android Studio designed to significantly accelerate the code, build and run cycle performed when developing an app. The Apply Changes feature is able to push updates to the running application, in many cases without the need to re-install or even restart the app. Apply Changes provides a number of different levels of support depending on the nature of the modification being applied to the project.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Android Studio 4.2 Development Essentials - Java Edition
Published in: Aug 2021 Publisher: Packt ISBN-13: 9781803238814
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime}