我将使用带有片段的导航框架,但在依赖项步骤中,当我尝试从文档添加 SafeArgs 时,我发现新的顶级 gradle 文件与文档不同,因此我无法添加它。您能解释一下如何将 SafeArgs 添加到新的 kotlin 顶级 gradle 文件吗?
我的顶级 gradle 文件:
plugins {
id 'com.android.application' version '7.1.2' apply false
id 'com.android.library' version '7.1.2' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false}
task clean(type: Delete) {
delete rootProject.buildDir}
Run Code Online (Sandbox Code Playgroud)
文档顶级 Gradle :
buildscript {
repositories {
google()
}
dependencies {
val nav_version = "2.4.1"
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version")
}}
Run Code Online (Sandbox Code Playgroud)
提前致谢。
使用它们后有没有办法清除 navArgs ?我有片段 A 用 navArgs 打开片段 B,然后我导航到片段 C 并且用户按回,所以片段 B 用相同的 navArgs 打开,我不想要那样。
有没有办法在没有 navArgs 的情况下导航回片段 B?
谢谢。
android kotlin android-architecture-navigation android-safe-args
我有一个片段:
class SomeFragment {
private val args by navArgs<SomeFragmentArgs>()
}
Run Code Online (Sandbox Code Playgroud)
该片段用于两个导航图中:
第一个_nav.xml
....
<fragment
android:id="@+id/initialFragment"
android:name="com.example.InitialFragment"
android:label="Initial Fragment">
<action
android:id="@+id/action_to_some_fragment"
app:destination="@id/someFragment" />
</fragment>
<fragment
android:id="@+id/someFragment"
android:name="com.example.SomeFragment"
android:label="Some Label">
<argument
android:name="someType"
app:argType="com.example.someType"/>
</fragment>
....
Run Code Online (Sandbox Code Playgroud)
第二个_nav.xml
....
<fragment
android:id="@+id/initialFragment2"
android:name="com.example.InitialFragment2"
android:label="Initial Fragment">
<action
android:id="@+id/action_to_some_fragment"
app:destination="@id/someFragment" />
</fragment>
<fragment
android:id="@+id/someFragment"
android:name="com.example.SomeFragment"
android:label="Some Label">
<argument
android:name="someType"
app:argType="com.example.someType"/>
</fragment>
....
Run Code Online (Sandbox Code Playgroud)
但是当我构建 R8 版本的项目时,会抛出:
R8:程序类型已存在:com.example.SomeFragmentArgs$Companion
谁可以帮我这个事?
我正在尝试在启用安全参数的不同导航图中重用片段。我注意到如果操作不同,我会收到编译错误。这是因为xxxFragmentDirections
自动生成的代码只会生成其中一项操作。
在nav_graph_1.xml 中:
<navigation
...
<fragment
android:id="@+id/myFragment"
android:name="com.example.android.MyFragment">
<action
android:id="@+id/next_action"
app:destination="@+id/dest_one" />
</fragment>
...
Run Code Online (Sandbox Code Playgroud)
在nav_graph_2.xml 中:
<navigation
...
<fragment
android:id="@+id/myFragment"
android:name="com.example.android.MyFragment">
<action
android:id="@+id/other_action"
app:destination="@+id/other_dest" />
</fragment>
...
Run Code Online (Sandbox Code Playgroud)
一个简单的用例:一个有两个流程的银行应用程序:取款和存款,因此你可以有两个导航图。你可以有一个AmountFragment
你可以只输入一个数字的地方,这可以重复用于提款或存款。但是,根据流程的不同,操作/目的地可能会有所不同。
那么,如何重用这个片段呢?
android android-fragments android-architecture-components android-architecture-navigation android-safe-args
我有一个ActionBar
菜单图标,可以打开一个CategoryFragment
. 该片段接受从另一个片段传递的category
对象SafeArgs
参数。在CategoryFragment
我保存category
的姓名和身份证到分享的内容片段ViewModel
作为SavedStateHandle
值。我已经对其进行了设置,以便片段SavedStateHandle
在需要时使用类别名称和 ID的存储值。例如,第一次CategoryFragment
使用从发送片段传递的类别对象,但随后的创建CategoryFrgament
将使用这些SavedStateHandle
值。
问题是,如果在第一次打开CategoriesFragment
然后通过按下手机的物理后退按钮或从导航CategoryFragment
栏中手机的最近按钮终止应用程序后退出应用程序,现在通过按下 ActionBar 菜单图标直接打开显示一个空白屏幕。这是因为从返回的值SavedStateHandle
是空的。我怎样才能解决这个问题?
类别片段
class CategoryFragment : Fragment(), SearchView.OnQueryTextListener {
lateinit var navController: NavController
private var adapter: TasksRecyclerAdapter? = null
private val viewModel: CategoryTasksViewModel by activityViewModels()
private var fromCategoriesFragment: Boolean = false
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the …
Run Code Online (Sandbox Code Playgroud) android android-savedstate android-architecture-components android-safe-args
我正在使用 android 导航组件,但出现错误配置根项目“AndroidCodingChallenge”时出现问题。
无法解析配置“:classpath”的所有工件。找不到 android.arch.navigation:navigation-safe-args-gradle-plugin:2.3.5。在以下位置进行了搜索:
在我的 app.gradle 下面
plugins {
id 'com.android.application'
id 'kotlin-android'
}
apply plugin: 'androidx.navigation.safeargs.kotlin'
apply plugin: 'kotlin-kapt'
apply from: '../commons.gradle'
android {
compileSdkVersion 30
buildFeatures {
dataBinding = true
// for view binding:
// viewBinding = true
}
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.androidcodingchallenge"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables{
useSupportLibrary = true
}
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility …
Run Code Online (Sandbox Code Playgroud) 我在使用 gradlew 和 Java 17 构建 Android 项目时遇到问题。我收到的错误消息是:
“无法解析 androidx.navigation:navigation-safe-args-gradle-plugin - '在编译时使用的组件,与 Java 17 兼容,而消费者需要在运行时使用的组件,与 Java 13 兼容'”
但是,当我通过 Android Studio 构建并运行应用程序时,一切正常。作为 CI 流程的一部分,此错误也会出现在我的 Git Actions 工作流程中。
我的项目需要 Java 版本 17。谁能帮助我理解为什么使用 gradlew 时会出现此错误以及如何解决它?
此外,我很好奇 Android Studio 正在运行的确切命令,因为该项目使用 GUI 成功构建了。了解该命令可能有助于深入了解该问题。
完整信息:
MacBook-Pro:My-Android me$ ./gradlew
Starting a Gradle Daemon (subsequent builds will be faster)
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'My-Android'.
> Could not resolve all files for configuration ':classpath'.
> …
Run Code Online (Sandbox Code Playgroud) 使用 Android 导航组件。我有两个导航图(每个都有自己的流程),比如说导航图 A 和 B。我成功地从 A 导航到 B,但我无法设法将图 A 中包含的最后一个 Fragment 的参数传递给属于图 B 的起始片段。
我能够在属于同一图表的片段之间传递参数,但在导航图表之间导航时不会生成设置参数的函数。
我正在尝试使用 safeargs 来实现这一点。
这是导航图代码:
导航图A:
<navigation android:id="@+id/nav_graph_a"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:startDestination="@id/fragment1">
<fragment
android:id="@+id/fragment1"
android:name="com.mypackage.fragments.Fragment1"
android:label="Fragment1">
<action
android:id="@+id/action_fragment1_to_fragment2"
app:destination="@id/fragment2"/>
</fragment>
<fragment
android:id="@+id/fragment2"
android:name="com.mypackage.fragments.Fragment2"
android:label="Fragment2">
<argument
android:name="thisArgumentAlwaysArrive"
android:defaultValue="null"
app:argType="string"/>
<action
android:id="@+id/action_fragment2_to_nav_graph_b"
app:destination="@id/nav_graph_b"/>
</fragment>
<include app:graph="@navigation/nav_graph_b"/>
Run Code Online (Sandbox Code Playgroud)
导航图B:
<navigation android:id="@+id/nav_graph_b"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:startDestination="@id/fragment3">
<fragment
android:id="@+id/fragment3"
android:name="com.mypackage.fragments.Fragment3"
android:label="Fragment3">
<argument
android:name="thisArgumentNeverArrive"
app:argType="string"/>
</fragment>
Run Code Online (Sandbox Code Playgroud)
关于如何实现这一目标有什么想法吗?
我已将导航框架从 2.3.5 更新到 2.4.0-alpha01 版本。我将它与 Safe Args gradle 插件一起使用。更新后,我开始出现'getActionId' overrides nothing
错误'getArguments' overrides nothing
。
我认为发生这种情况是因为 Safe Args在生成 Direction 类时不正确地覆盖了actionId
和arguments
字段。NavDirections
例如,这是正在生成的内容:
public override fun getActionId(): Int =
R.id.action_navigation_articles_to_articleReaderFragment
Run Code Online (Sandbox Code Playgroud)
这是应该生成的:
public override val actionId: Int = R.id.action_navigation_articles_to_articleReaderFragment
Run Code Online (Sandbox Code Playgroud)
如何获得安全参数来生成正确的构建器类?
android android-navigation android-architecture-navigation android-safe-args
实际上 DetailActivity(需要传递参数)存在于库模块中,我需要从应用程序(FragmentA)启动它,但我收到构建错误,如下所示:
Too many arguments for public final fun actionFragmentAToDetailActivity()
Run Code Online (Sandbox Code Playgroud)
我正在使用 SafeArgs 库来传递数据。
片段A
findNavController().navigate(
FragmentADirections.actionFragmentAToDetailActivity(
"id",
"name"
)
)
Run Code Online (Sandbox Code Playgroud)
nav_graph_app.xml(存在于应用程序模块中)
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_a_navigation"
app:startDestination="@id/fragmentA">
<fragment
android:id="@+id/fragmentA"
android:name="com.abhishek.fragmentA"
android:label="WeeklyWorkListFragment"
tools:layout="@layout/fragment_a">
<action
android:id="@+id/action_fragmentA_to_DetailActivity"
app:destination="@id/detail_activity" />
</fragment>
</navigation>
Run Code Online (Sandbox Code Playgroud)
nav_graph_common_component.xml(存在于库项目中,作为库模块包含在应用程序的 build.gradle 中)
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/common_component_navigation"
<activity
android:id="@+id/detail_activity"
android:name="com.components.DetailActivity"
android:label="DetailActivity"
tools:layout="@layout/activity_detail">
<argument
android:name="id"
app:argType="string" />
<argument
android:name="name"
app:argType="string" />
</activity>
</navigation>
Run Code Online (Sandbox Code Playgroud)
任何帮助或指导将不胜感激。
android android-architecture-navigation android-safe-args android-navigation-graph
android ×9
android-architecture-navigation ×6
gradle ×3
kotlin ×3
android-architecture-components ×2
navigation ×1