You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Navigation中使用Safe Args传递Long类型值失败如何解决?

Fixing Long Type Parameter Issues with Android Navigation Safe Args

Absolutely, you can pass Long (or long) type arguments using Safe Args—let’s get to the bottom of why your setup is generating String types instead, and fix it step by step.

First, Confirm Your Safe Args Setup is Up-to-Date

Older versions of the Safe Args plugin had limited support for primitive types, so make sure you’re using a recent stable version.

  1. Project-level build configuration (in build.gradle or settings.gradle for newer Android Studio projects):
    buildscript {
        dependencies {
            classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.7.5" // Use the latest stable version
        }
    }
    
  2. App-module build configuration:
    At the top of your app’s build.gradle file, apply the plugin:
    plugins {
        id 'com.android.application'
        id 'androidx.navigation.safeargs'
        // Add 'androidx.navigation.safeargs.kotlin' if you're using Kotlin
    }
    

Correct Navigation Graph Parameter Configuration

Your existing XML is close, but double-check the app:type value and add optional nullability if needed (this helps the plugin generate the correct type):

<fragment android:id="@+id/myFragment"
    android:name="com.x.app.y.MyFragment"
    android:label="fragment_mine"
    tools:layout="@layout/fragment_mine">
    <argument
        android:name="row_id"
        app:type="long"
        app:nullable="false" /> <!-- Set to true if the parameter can be null -->
</fragment>

The app:type="long" (lowercase) tells Safe Args to generate a long (Java) or Long (Kotlin) type for the parameter.

Fixing Incorrect Generated Code (Cache Issue)

If you’re still seeing String types after fixing the configuration, this is almost always a build cache problem:

  1. Go to Build > Clean Project in Android Studio
  2. Then run Build > Rebuild Project
    This forces the Safe Args plugin to regenerate the navigation classes from scratch, using your updated parameter configuration.

Using the Generated Long Parameter

Once the code is regenerated correctly, you can use it like this:

For Java:

// Navigate to MyFragment with a Long parameter
MyFragmentDirections.ActionToMyFragment action = MyFragmentDirections.actionToMyFragment(123L);
Navigation.findNavController(view).navigate(action);

// Retrieve the parameter in MyFragment
long rowId = MyFragmentArgs.fromBundle(getArguments()).getRowId();

For Kotlin:

// Navigate to MyFragment with a Long parameter
val action = MyFragmentDirections.actionToMyFragment(123L)
findNavController().navigate(action)

// Retrieve the parameter in MyFragment
val args = MyFragmentArgs.fromBundle(requireArguments())
val rowId = args.rowId

Supported Safe Args Types (for Reference)

To avoid confusion later, here’s a quick list of supported types and their generated counterparts:

  • string → String
  • integer → int (Java) / Int (Kotlin)
  • long → long (Java) / Long (Kotlin)
  • float → float (Java) / Float (Kotlin)
  • boolean → boolean (Java) / Boolean (Kotlin)
  • reference → int (for resource IDs)
  • custom → Custom Parcelable/Serializable types

内容的提问来源于stack exchange,提问作者Topper Harley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:23:34