当前位置:网站首页>Alibaba internal jetpack Treasure Book flows out unexpectedly! The ultimate classic, can be called the ceiling of Android architecture components

Alibaba internal jetpack Treasure Book flows out unexpectedly! The ultimate classic, can be called the ceiling of Android architecture components

2020-11-10 12:44:30 The heat is you

brief introduction

Jetpack It's a library 、 Tools and guidelines , It can help developers write quality applications more easily . These components help developers follow best practices 、 Let developers get rid of the work of writing boilerplate code and simplify complex tasks , So that developers can focus on the code they need .

Android Jetpack What is the role of a component ?

  • Navigation: One for management Fragment Switch tool class , visualization 、 Bindable controls 、 Supporting animation is its advantage .
  • Data Binding: Needless to say , We all know , Speed up MVVM The creation of .
  • Lifecycle: He is that we can deal with Activity and Fragment Important reasons for the life cycle of , stay AndroidX Of Fragment and Activity Have been to Lifecycle Provides default support .
  • ViewModel: treat as MVVM Of ViewModel layer , And has the declaration cycle consciousness processing and UI Relevant data .
  • LiveData: Same as RxJava It does the same thing , Monitor data , The advantage is that you don't have to deal with the lifecycle 、 No memory leak, etc .
  • Room: Powerful ORM Database framework .
  • Paging: Easy to use data paging Library , Support RecyclerView.WorkManager: flexible 、 Simple 、 A background task processing library that delays and guarantees execution .

You may choose Android Jetpack Why

The following may be your choice Android Jetpack Why :

  • It's more convenient to use together : because Android Jetpack Many of the libraries in the component support other libraries , such as Room and Paging That's right LiveData Provided support .
  • Backward compatibility : Basically every component provides support for lower versions .
  • Support RxJava: because RxJava Strong ecological environment , Almost all the components related to data are right for RxJava Provided support .
  • Reduce the amount of code : With Data Binding + ViewModel + LiveData or RxJava Built MVVM Patterns can significantly reduce the amount of code , It's usually used MVP Patterns will also be more convenient , There is no need to actively update UI.
  • There's no need to tie :Android Jetpack Series components can be used without bundling , If you just want to use a single library inside , Then you can rely on just one library .

Android Jetpack Do you want to learn ?

learn , You must learn ,Android Jetpack For Google , This is a manifestation of their determination to reorganize and unify the Android ecosystem ,Android Jetpack What's shown is , It's also the direction that Google wants to expand and maintain . In the long-term suffering from the vast selection of third-party libraries Android For developers , This is a bright light that Google provides us .

Maybe ,Android Jetpack Some of the libraries in the series are not mature enough to be your first choice for work , however , Learn more Android Jetpack The component will provide you with the opportunity to communicate with the big guy in Google , Bring some ideas to your daily development .

How to learn ?

If you are due to a lack of learning materials , And I happened to collect this inside Alibaba Jetpack Treasure , From entry to mastery , The course is easy to understand , Examples abound , Have the basic knowledge , There are also advanced skills , It can help readers get started quickly , It's you who study Jetpack The treasure of sunflower .

Friends, if you need a full set of 《Jetpack Architecture components go from beginner to proficient 》, Draw directly to the end of the article for free ~

Android Jetpack - Navigation

Navigation Direct translation is navigation , It is Android Jetpack One of the components , Let alone Activity Application becomes the preferred Architecture . In application Fragment The jump of the page is from Navigation To deal with it , Developers don't have to deal with FragmentTransaction The complexity and related transition animation .

 Catalog

 The actual part of the screenshot

Android Jetpack - Data Binding

Data Binding Is a support library , It uses a kind of The way the statement is made Instead of The way of coding , Used to bind UI Controls and data sources .
Usually we will be in activity It calls UI Method declaration of framework layer view. for example , The following code calls findViewById() Declared a TextView Control , And put it with viewModel Of userName Attribute is bound :

findViewById<TextView>(R.id.sample_text).apply {
    text = viewModel.userName
}

The following code shows how to use Data Binding In the layout, directly give TextView Of test Property to assign . The advantage of this is that you don't have to call Java Code . Note that the syntax used in assignment expressions is @{}:

<TextView
    android:text="@{viewmodel.userName}" />

Bind directly in the layout UI Control , Can be reduced in activity in UI Calling framework methods , This makes the code simpler 、 Easier to maintain . It can also improve App Performance of , Avoid memory leaks and null pointer exceptions .

 Catalog

 The actual part of the screenshot

Android Jetpack - ViewModel & LiveData

ViewModel Separate the view from the logic .Activity perhaps Fragment Only responsible for UI Display part . Specific network requests or database operations are ViewModel be responsible for . Be similar to MVP In the pattern Presenter layer .ViewModel Class is designed to store and manage interface related data in a lifecycle focused manner . Allows data to be retained after configuration changes such as screen rotation . We know that changes in configuration items such as rotating screens can cause our Activity Destroyed and rebuilt , here Activity The data held will be lost with it , and ViewModel It will not be destroyed , So it can help us save data in the process . also ViewModel Don't hold View Examples of layers , adopt LiveData And Activity perhaps Fragment Communications , Don't worry about potential memory leaks .

LiveData Is an observable data storage class . Unlike conventional observable classes ,LiveData Life cycle awareness , It means that it follows other application components ( Such as ActivityFragment or Service) Life cycle of . This perception ensures that LiveData When the data source changes , Notify its observers of updates UI Interface . At the same time, it will only notify the Active Observer update interface of status , If an observer's state is Paused or Destroyed Then it will not be notified . So don't worry about memory leaks .

 Catalog

 The actual part of the screenshot

Android Jetpack - Room

Room yes Jetpack A member of the component library , Belong to ORM library , It's mainly about Sqlite Made a layer of abstraction , So as to simplify the operation of database by developers .Room Support syntax checking at compile time , And support the return of LiveData.

Add dependency

stay app Of build.gradle Add the following dependency :

def room_version = "2.2.0-rc01"
    
implementation "androidx.room:room-runtime:$room_version"
// For Kotlin use kapt instead of annotationProcessor ( Pay attention to this note )
kapt "androidx.room:room-compiler:$room_version"

If the project is using Kotlin Language to develop , Adding room-compiler When you use kapt keyword ,java Language development on the use of annotationProcessor The key . Otherwise, it will lead to access error .

 Catalog

 Project practice

Android Jetpack - Paging

Many applications get data from data sources that contain a large number of projects , But only a small part of the data is displayed at a time . Loading the data displayed in the application can be large and costly , So avoid one download , Creating or rendering too much data . In order to make it easier to gradually load data into our applications, Google methods provides this component , It's easy to load and now big datasets with our RecyclerView Fast , Infinite scrolling . It can be stored locally , The network or both loads paging data , And it allows us to customize how to load the content . It can work with Room,LiveData and RxJava Use it together .

Paging Libray Divided into three parts :DataSource, PagedList, PagedAdapter

 Catalog

 The actual part of the screenshot

Android Jetpack - WorkManger

WorkManager yes Android Jetpack The component that manages background tasks in .

Common use scenarios :1. Sending logs or analysis data to back-end services 2. Periodically synchronize application data with the server

Use WorkManager API You can easily schedule background tasks . Can be delayed to run ( It doesn't need to run immediately ) And quit the app ( Process not closed ) Or tasks that can run reliably when the application is restarted .

 Catalog

 The actual part of the screenshot

Android Jetpack Architecture components Lifecycle

For the security of the application , There is often a need for security confirmation such as software confirmation when switching back from the background to the front desk . If it was before , This demand is actually very difficult to achieve . But since Google Introduction Lifecycle After component , This requirement is much simpler .Lifecycle In addition to perception from the background back to the front desk , Using this component, it is easier to implement complex life cycle operations

 Catalog

 Partial screenshots

Android Jetpack Compose The best guide to getting started

Jetpack Compose Is one for building native Android UI A modern toolkit for , It's based on a declarative programming model , So you can simply describe UI The appearance of the , and Compose The rest of the work - When the state changes , Yours UI Will automatically update . because Compose be based on Kotlin structure , So you can Java Programming languages are fully interoperable , And have direct access to all AndroidJetpack API. It's with the existing UI The toolkit is also fully compatible , So you can mix the original View And now the new View, And use it from the beginning Material And animation design .

 Catalog

 The actual part of the screenshot

Conclusion

Because of the limited space of the article , Show only part of the content , This note also contains Android Jetpack Architecture components --App Startup、Android Jetpack Introduction to the latest components 、Android Jetpack Project practice ( from 0 build Jetpack Version of WanAndroid client )、 Project practice and so on .

Friends, if you need to , Yes, mine 【Github】 Read Download .

Click on me to get it for free

版权声明
本文为[The heat is you]所创,转载请带上原文链接,感谢