这次我需要你的帮助来使用带有深度链接的 android 导航组件。
我一直在关注这个文档,片段和深层链接之间的连接工作正常。
问题在于接收深层链接的活动。就我而言,我设置了 android:launchMode="singleTask"
<activity android:name=".features.welcome.WelcomeActivity"
android:launchMode="singleTask">
<nav-graph android:value="@navigation/welcome_nav_graph" />
</activity>
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
Timber.d("onNewIntent: $intent with activity: $this")
navController.handleDeepLink(intent)
}
Run Code Online (Sandbox Code Playgroud)
通过这种配置,我注意到了一些奇怪的行为:
每次单击深层链接时,WelcomeActivity 都会收到两次 onNewIntent 调用。甚至有时会创建该活动的新实例..就像
1_ object1-onNewIntent
2_ object1-onNewIntent
3_ object2-onCreate
这里有一些日志:
首次发布
onCreate:意图 { flg=0x10000000 cmp={applicationId}/{package}.WelcomeActivity } 活动:{package}.WelcomeActivity@4adbef0
打开深层链接
onNewIntent: Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=https://{depp_link}… flg=0x10010000 cmp={applicationId}/{package}.WelcomeActivity(有附加)} 与活动:{package}.WelcomeActivity@4adbef0
onNewIntent: Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=https://{depp_link}... flg=0x1001c000 cmp={applicationId}/{package}.WelcomeActivity (有额外的)} 与活动:{package}.WelcomeActivity@4adbef0
onCreate: Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=https://{depp_link}... flg=0x1001c000 cmp={applicationId}/{package}.WelcomeActivity (有额外的)} 与活动:{package}.WelcomeActivity@b77c6b
杀死应用程序并打开深层链接
onCreate: Intent …
android android-jetpack android-architecture-navigation android-jetpack-navigation
我有下一个疑问:我正在开发一个需要启动前台服务的应用程序。我知道从 Android O 开始,您需要向该前台服务附加通知。到现在为止还挺好。
问题是:如果用户从设置中禁用该通知会发生什么?我一直在测试该应用程序,似乎一切正常,但我想在互联网上仔细检查,但找不到任何东西。
如果他/她想要该功能(需要前台服务),我是否应该强制用户启用通知?
有没有人对此有一些见解?
我正在努力将Dagger 2设置到我的android项目中.这是我第一次使用这个框架,到目前为止一切顺利.但是我在你的项目中设置这个框架的方式看到了不同的方法,我想知道哪一个更好,因为我比较两者,对我来说结果有点相同.
我遵循了这个指南:https://github.com/codepath/android_guides/wiki/Dependency-Injection-with-Dagger-2
在互联网上搜索所有这些都使用这种方法.它使用@Module和@Component来定义依赖项.
你的申请最终如下:
public class MyApp extends Application {
private NetComponent mNetComponent;
@Override
public void onCreate() {
super.onCreate();
// Dagger%COMPONENT_NAME%
mNetComponent = DaggerNetComponent.builder()
// list of modules that are part of this component need to be created here too
.appModule(new AppModule(this)) // This also corresponds to the name of your module: %component_name%Module
.netModule(new NetModule("https://api.github.com"))
.build();
// If a Dagger 2 component does not have any constructor arguments for any of its modules,
// then we …Run Code Online (Sandbox Code Playgroud) 我决定在 android 上尝试材质警报对话框。我遇到的问题是当我尝试应用某些样式时。检查文档,我发现了这个:
<item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog</item>
Run Code Online (Sandbox Code Playgroud)
所以我决定试试这个:
<style name="BaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="materialAlertDialogTheme">@style/Theme.App.MaterialDialogAlert</item>
</style>
<style name="Theme.App.MaterialDialogAlert" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<!-- FIXME: this does not work. it does not change the title appearance. -->
<!-- <item name="materialAlertDialogTitleTextStyle">@style/TextAppearance.App.MaterialDialogAlert.Title.Text</item>-->
<!-- FIXME: this change only the title font, not the message appearance -->
<item name="android:fontFamily">@font/nunito_semi_bold</item>
<item name="buttonBarPositiveButtonStyle">@style/Widget.App.Button.TextButton</item>
<item name="buttonBarNegativeButtonStyle">@style/Widget.App.Button.TextButton</item>
</style>
<style name="TextAppearance.App.MaterialDialogAlert.Title.Text" parent="MaterialAlertDialog.MaterialComponents.Title.Text">
<item name="android:fontFamily">@font/nunito_semi_bold</item>
</style>
Run Code Online (Sandbox Code Playgroud)
但到目前为止,我可以成功地设置按钮样式。对于标题和消息,我有一些问题。现在我可以通过以下方式将一些样式应用于标题:
<item name="android:fontFamily">@font/nunito_semi_bold</item>
Run Code Online (Sandbox Code Playgroud)
但我不认为这是正确的方法。首先,这不会将字体应用于消息部分。其次,我想应用其他文本样式,一种用于标题,一种用于消息部分。
我也试过检查这种风格:MaterialAlertDialog.MaterialComponents.Title.Text我可以看到这个项目在某个时候被应用:
<item name="android:textAppearance">?attr/textAppearanceSubtitle1</item>
Run Code Online (Sandbox Code Playgroud)
所以我决定在我的基本主题中声明:
<style name="BaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="textAppearanceSubtitle1">@style/TextAppearance.App.Subtitle1</item>
</style>
<style name="TextAppearance.App.Subtitle1" parent="TextAppearance.MaterialComponents.Subtitle1">
<item …Run Code Online (Sandbox Code Playgroud) android android-alertdialog material-design material-components material-components-android
尝试使用来自 AS 的maven-publish插件时遇到问题。
我在一个项目中尝试了这个例子,它没有问题。但是一旦我转向 kotlin dsl,我就会遇到这个问题:
SoftwareComponentInternal with name 'release' not found.
Run Code Online (Sandbox Code Playgroud)
这是我第一次处理 kotlin dsl。首先,我不知道您是否可以同时拥有 kotlin dsl 和 groovy,但我第一次尝试时只是将 kotlin dsl 添加到根目录和 app:build.gradle 中。我有这个错误,所以我决定也将库迁移到 kotlin dsl: mylib:build.gradle。我结束了这个代码:
plugins {
id(BuildPlugins.androidLibrary)
id(BuildPlugins.kotlinAndroid)
id(BuildPlugins.kotlinAndroidExtensions)
id(BuildPlugins.mavenPublish)
}
afterEvaluate {
publishing {
publications {
// Creates a Maven publication called "release".
create<MavenPublication>("release") {
// Applies the component for the release build variant.
from(components["release"])
// You can then customize attributes of the publication as shown below.
groupId = "com.mylib"
artifactId = "alpha"
version …Run Code Online (Sandbox Code Playgroud) 我正在使用导航组件和底部导航
val navController = indNavController(R.id.nav_host_fragment)
bottom_navigation.inflateMenu(R.menu.bottom_navigation_menu)
bottom_navigation.setupWithNavController(navController)
Run Code Online (Sandbox Code Playgroud)
我面临下一个问题:
当在底部导航中选择一个项目时,就会加载一个片段。当我再次按下同一个项目时,问题就出现了,然后会加载一个新的片段,这根本没有意义。
示例: 1- 用户选择菜单项 A,然后加载 FragmentA。2- 用户再次选择菜单项 A,然后将加载一个新的 FragmentA,
我试图使用
bottom_navigation.setOnNavigationItemSelectedListener { }
Run Code Online (Sandbox Code Playgroud)
但是底部导航将无法与 navController 一起使用。
所以问题是:有一种方法可以处理这种情况,以便在用户已经在该屏幕上时再次加载新片段?
目前我正在使用材料组件和材料主题:
<style name="BaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
...
...
<item name="materialButtonStyle">@style/Button</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我能够使用 materialButtonStyle 为每个按钮定义样式,我想知道是否可以为工具栏实现相同的样式。
这真的很重要:这个想法是为了避免在应用程序栏或工具栏组件中定义样式/主题,而只是使用它来获取应用程序主题定义的样式。
我想避免这种情况:
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:theme="@style/ToolbarTheme">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextAppearance="@style/ToolbarTextAppearance"
tools:title="Title" />
</com.google.android.material.appbar.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)
在主题中,我希望能够指定字体样式和文本外观。
这些是我的风格:
<style name="ToolbarTheme" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar">
</style>
<style name="ToolbarTextAppearance" parent="TextAppearance.AppCompat.Title">
<item name="android:fontFamily">@font/nunito_bold</item>
</style>
Run Code Online (Sandbox Code Playgroud)
使用之前定义的代码,我能够获得这些更改。但正如我所提到的,我想将其定义为我的应用程序主题的一部分,例如材质按钮。
我尝试使用它,但没有成功:
<style name="BaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
...
<item name="appBarLayoutStyle">@style/ToolbarTheme</item>
<item name="toolbarStyle">@style/ToolbarTheme</item>
</style>
Run Code Online (Sandbox Code Playgroud) android android-theme android-toolbar material-components material-components-android
android ×7
material-components-android ×2
android-architecture-navigation ×1
dagger-2 ×1
gradle ×1
kotlin ×1