(一百八十九)Android Jetpack 学习(一)

2023-12-19 07:32

本文主要是介绍(一百八十九)Android Jetpack 学习(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言:老大让学习点新的东西分享一下,本来看了WiFi的电量统计,看到最后感觉挺没意思的,然后就翻bos直聘看有什么新技术要求的,看到flutter,Jetpack、react native。。。Jetpack这个比较新,学习下Jetpack

 

参考:

https://developer.android.google.cn/jetpack

https://developer.android.google.cn/jetpack/docs/getting-started/

下面这个是官方整理的应用开发指导?

https://developer.android.google.cn/guide

 

1. 简介

Jetpack 是一套库、工具和指南,可帮助开发者更轻松地编写优质应用。这些组件可帮助您遵循最佳做法、让您摆脱编写样板代码的工作并简化复杂任务,以便您将精力集中放在所需的代码上。

Jetpack 包含与平台 API 解除捆绑的 androidx.* 软件包库。这意味着,它可以提供向后兼容性,且比 Android 平台的更新频率更高,以此确保您始终可以获取最新且最好的 Jetpack 组件版本。

总体宣传了三大优势

 

2.Android Jetpack 组件

Android Jetpack 组件是库的集合,这些库是为协同工作而构建的,不过也可以单独采用,同时利用 Kotlin 语言功能帮助您提高工作效率。可全部使用,也可混合搭配!

主要分为四大部分组件

详细列出如下,用红色线条分割开来对应上面四大组件,Google官网可点击查看详细

 

3. Android Jetpack 使用入门

Jetpack 包含一系列 Android 库,它们都采用最佳做法并在 Android 应用中提供向后兼容性。

Jetpack 应用架构指南概述了构建 Android 应用时要考虑的最佳做法和推荐架构。

下文介绍了如何开始使用 Jetpack 组件。

3.1 在应用中使用 Jetpack 库

所有 Jetpack 组件都可在 Google Maven 代码库中找到。

打开您的项目的 build.gradle 文件并添加 google() 代码库,如下所示:

    allprojects {repositories {google()jcenter()}}

 

然后,您可以添加 Jetpack 组件,例如作为 Lifecycles 库的一部分的 LiveData 和 ViewModel 等架构组件,如下所示:

    dependencies {def lifecycle_version = "2.0.0"implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"// Optional : Kotlin extension (https://d.android.com/kotlin/ktx)implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"...}

 

许多 Jetpack 库还提供 Android KTX 扩展程序,如上面的 lifecycle-viewmodel-ktx 所示。KTX 扩展程序以基于 Java 的 API 为基础,充分利用了 Kotlin 特有的语言功能。

如需了解新的 Jetpack 库版本,请查看版本页面。

基于 Kotlin 以及基于 Java 的 API 参考页面适用于所有 Jetpack 库。

有点不妙,google()这个仓还被墙吗?动手试下

 

3.1.1 Unable to resolve dependency

Could not resolve com.android.support:appcompat-v7:29

修改了最低的Android 版本为p,然后随便创建一个module然后就报错了。。。

方案一:

参考https://blog.csdn.net/afireswallow/article/details/91129831

感觉解决的方式不大对,改了依赖的版本,走偏门的不靠谱

 

方案二:

https://blog.csdn.net/Camille05/article/details/96697638

这个感觉靠谱,我的sdk tools版本和platform version明显错位了

之后右键module弹出module Settings改变buildToolsVersion

buildToolsVersion '28.0.3'

这时发现29是Q的版本,不知道什么时候选错了,都改成28还是报错,算了,还是先按方案一来吧

待续

 

ERROR: Unable to resolve dependency for ':demo_189_jetpack@debug/compileClasspath': Could not resolve androidx.lifecycle:lifecycle-extensions:2.0.0.
Show Details
Affected Modules: demo_189_jetpack


ERROR: Unable to resolve dependency for ':demo_189_jetpack@debug/compileClasspath': Could not resolve androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0.
Show Details
Affected Modules: demo_189_jetpack


ERROR: Unable to resolve dependency for ':demo_189_jetpack@debugAndroidTest/compileClasspath': Could not resolve androidx.lifecycle:lifecycle-extensions:2.0.0.
Show Details
Affected Modules: demo_189_jetpack

导入入门的依赖,恩,意料之中resolve dependency失败,开发环境真是。。。。。。

待续

思路:

https://blog.csdn.net/weixin_40849588/article/details/86559842

https://blog.csdn.net/weixin_40849588/article/details/90575618

 

4. 组件体验

4.1 lifecycle

https://developer.android.google.cn/topic/libraries/architecture/lifecycle

Lifecycle-aware components perform actions in response to a change in the lifecycle status of another component, such as activities and fragments. These components help you produce better-organized, and often lighter-weight code, that is easier to maintain.

A common pattern is to implement the actions of the dependent components in the lifecycle methods of activities and fragments. However, this pattern leads to a poor organization of the code and to the proliferation of errors. By using lifecycle-aware components, you can move the code of dependent components out of the lifecycle methods and into the components themselves.

我们之前写和activity fragment生命周期强相关的代码的时候,比如注册广播,总是会在生命周期了罗列很多代码,让代码结构变得繁杂后续难以维护,现在有个lifecycle可以优化这种场景使用。

Moreover, there's no guarantee that the component starts before the activity or fragment is stopped. This is especially true if we need to perform a long-running operation, such as some configuration check in onStart(). This can cause a race condition where the onStop() method finishes before the onStart(), keeping the component alive longer than it's needed.

另外有时候我们在生命周期里执行耗时操作的时候activity或者fragment被干掉了就会有问题。

这个让我想到了Settings,WiFi里的WifiTracker就是这么用的,之前梳理过

(一百三十九)Android P 结合WifiSettings学习WifiTracker的生命周期

 

A common use case is to avoid invoking certain callbacks if the Lifecycle isn't in a good state right now. For example, if the callback runs a fragment transaction after the activity state is saved, it would trigger a crash, so we would never want to invoke that callback.

To make this use case easy, the Lifecycle class allows other objects to query the current state.

KOTLINJAVA

class MyLocationListener implements LifecycleObserver {private boolean enabled = false;public MyLocationListener(Context context, Lifecycle lifecycle, Callback callback) {...}@OnLifecycleEvent(Lifecycle.Event.ON_START)void start() {if (enabled) {// connect}}public void enable() {enabled = true;if (lifecycle.getCurrentState().isAtLeast(STARTED)) {// connect if not connected}}@OnLifecycleEvent(Lifecycle.Event.ON_STOP)void stop() {// disconnect if connected}
}

 

With this implementation, our LocationListener class is completely lifecycle-aware. If we need to use our LocationListener from another activity or fragment, we just need to initialize it. All of the setup and teardown operations are managed by the class itself.

还有个比较好的是实现LifecycleObserver的类通过使用lifecycle.getCurrentState可以完全知晓当前activity或者fragment当前的状态?

getCurrentState是public的,状态是枚举类型

    public enum Event {/*** Constant for onCreate event of the {@link LifecycleOwner}.*/ON_CREATE,/*** Constant for onStart event of the {@link LifecycleOwner}.*/ON_START,/*** Constant for onResume event of the {@link LifecycleOwner}.*/ON_RESUME,/*** Constant for onPause event of the {@link LifecycleOwner}.*/ON_PAUSE,/*** Constant for onStop event of the {@link LifecycleOwner}.*/ON_STOP,/*** Constant for onDestroy event of the {@link LifecycleOwner}.*/ON_DESTROY,/*** An {@link Event Event} constant that can be used to match all events.*/ON_ANY}

具体api可以参考

http://androidxref.com/9.0.0_r3/xref/frameworks/support/lifecycle/common/src/main/java/androidx/lifecycle/Lifecycle.java

 

Fragments and Activities in Support Library 26.1.0 and later already implement the LifecycleOwner interface.

搜了下Android P的源码里已经继承了,所以WifiSettings可以直接用getLifecycle

 

实践指导

Best practices for lifecycle-aware components

  • Keep your UI controllers (activities and fragments) as lean as possible. They should not try to acquire their own data; instead, use a ViewModel to do that, and observe a LiveData object to reflect the changes back to the views.
  • Try to write data-driven UIs where your UI controller’s responsibility is to update the views as data changes, or notify user actions back to the ViewModel.
  • Put your data logic in your ViewModel class. ViewModel should serve as the connector between your UI controller and the rest of your app. Be careful though, it isn't ViewModel's responsibility to fetch data (for example, from a network). Instead, ViewModel should call the appropriate component to fetch the data, then provide the result back to the UI controller.
  • Use Data Binding to maintain a clean interface between your views and the UI controller. This allows you to make your views more declarative and minimize the update code you need to write in your activities and fragments. If you prefer to do this in the Java programming language, use a library like Butter Knife to avoid boilerplate code and have a better abstraction.
  • If your UI is complex, consider creating a presenter class to handle UI modifications. This might be a laborious task, but it can make your UI components easier to test.
  • Avoid referencing a View or Activity context in your ViewModel. If the ViewModel outlives the activity (in case of configuration changes), your activity leaks and isn't properly disposed by the garbage collector.
  • Use Kotlin coroutines to manage long-running tasks and other operations that can run asynchronously.

例子

Use cases for lifecycle-aware components

Lifecycle-aware components can make it much easier for you to manage lifecycles in a variety of cases. A few examples are:

  • Switching between coarse and fine-grained location updates. Use lifecycle-aware components to enable fine-grained location updates while your location app is visible and switch to coarse-grained updates when the app is in the background. LiveData, a lifecycle-aware component, allows your app to automatically update the UI when your user changes locations.
  • Stopping and starting video buffering. Use lifecycle-aware components to start video buffering as soon as possible, but defer playback until app is fully started. You can also use lifecycle-aware components to terminate buffering when your app is destroyed.
  • Starting and stopping network connectivity. Use lifecycle-aware components to enable live updating (streaming) of network data while an app is in the foreground and also to automatically pause when the app goes into the background.
  • Pausing and resuming animated drawables. Use lifecycle-aware components to handle pausing animated drawables when while app is in the background and resume drawables after the app is in the foreground.

高低精度定位、视频缓冲、网络连接和动画都可以使用lifecycle-aware

 

例外

Handling on stop events

When a Lifecycle belongs to an AppCompatActivity or Fragment, the Lifecycle's state changes to CREATEDand the ON_STOP event is dispatched when the AppCompatActivity or Fragment's onSaveInstanceState() is called.

When a Fragment or AppCompatActivity's state is saved via onSaveInstanceState(), it's UI is considered immutable until ON_START is called. Trying to modify the UI after the state is saved is likely to cause inconsistencies in the navigation state of your application which is why FragmentManager throws an exception if the app runs aFragmentTransaction after state is saved. See commit() for details.

LiveData prevents this edge case out of the box by refraining from calling its observer if the observer's associated Lifecycle isn't at least STARTED. Behind the scenes, it calls isAtLeast() before deciding to invoke its observer.

Unfortunately, AppCompatActivity's onStop() method is called after onSaveInstanceState(), which leaves a gap where UI state changes are not allowed but the Lifecycle has not yet been moved to the CREATED state.

To prevent this issue, the Lifecycle class in version beta2 and lower mark the state as CREATED without dispatching the event so that any code that checks the current state gets the real value even though the event isn't dispatched until onStop() is called by the system.

Unfortunately, this solution has two major problems:

  • On API level 23 and lower, the Android system actually saves the state of an activity even if it is partially covered by another activity. In other words, the Android system calls onSaveInstanceState() but it doesn't necessarily call onStop(). This creates a potentially long interval where the observer still thinks that the lifecycle is active even though its UI state can't be modified.
  • Any class that wants to expose a similar behavior to the LiveData class has to implement the workaround provided by Lifecycle version beta 2 and lower.

Note: To make this flow simpler and provide better compatibility with older versions, starting at version 1.0.0-rc1Lifecycle objects are marked as CREATED and ON_STOP is dispatched when onSaveInstanceState() is called without waiting for a call to the onStop() method. This is unlikely to impact your code but it is something you need to be aware of as it doesn't match the call order in the Activity class in API level 26 and lower.

由于onSavedInstanceState之后不能改变UI,但是这时候activity或者fragment的生命周期还没走到onStop,这时候就会有bug,这里采用里一个workround的方案,当onSavedInstanceState调用的时候infecycle就被标记为create,on_stop事件就被传达出去。

更多资源学习

Additional resources

To learn more about handling lifecycles with lifecycle-aware components, consult the following additional resources.

Samples

  • Android Architecture Components Basic Sample
  • Sunflower, a demo app demonstrating best practices with Architecture Components

Codelabs

  • Android Lifecycle-aware components

Blogs

  • Introducing Android Sunflower

 

这篇关于(一百八十九)Android Jetpack 学习(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/511378

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

【学习笔记】 陈强-机器学习-Python-Ch15 人工神经网络(1)sklearn

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归 【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析 【学

系统架构师考试学习笔记第三篇——架构设计高级知识(20)通信系统架构设计理论与实践

本章知识考点:         第20课时主要学习通信系统架构设计的理论和工作中的实践。根据新版考试大纲,本课时知识点会涉及案例分析题(25分),而在历年考试中,案例题对该部分内容的考查并不多,虽在综合知识选择题目中经常考查,但分值也不高。本课时内容侧重于对知识点的记忆和理解,按照以往的出题规律,通信系统架构设计基础知识点多来源于教材内的基础网络设备、网络架构和教材外最新时事热点技术。本课时知识