首页 > 解决方案 > 从其他应用程序和喷气背包导航接收简单数据

问题描述

这可能是一个已经问过的问题,也可能是一个愚蠢的问题,但我找不到关于我的问题的答案。我正在做一个项目,我使用喷气背包导航组件进行导航,并通过意图过滤器从另一个应用程序接收一些数据。当我调试应用程序时,我看到我在 onCreate 中收到了数据,但是当我使用导航控制器将数据传递给片段(它不是起始目的地)时,片段的 onViewCreated 中的参数是无效的。我真的无法解释原因。问题是它首先导航到起始目的地,然后到那个片段,参数丢失了吗?我需要使用深度链接吗?问题出在其他地方吗?欢迎任何解释和解决方案。

提前致谢!

显现

<application
        android:name="com.example"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="AllowBackup">
        <activity
            android:name="com.ultimate.example.main.MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data
                    android:host="example.com"
                    android:mimeType="text/plain" />
            </intent-filter>
        </activity>
    </application>

主要活动

private val navController by lazy {
    Navigation.findNavController(this, R.id.navigation_host_fragment)
}

override fun onCreate(savedInstanceState: Bundle?) {
        setTheme(R.style.SplashTheme)
        super.onCreate(savedInstanceState)

        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
        setUpNavigation()
        handleIntent(intent)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        handleIntent(intent)
    }

    private fun handleIntent(intent: Intent?) {
        intent?.let {
            val action = it.action
            val type = it.type
            if (Intent.ACTION_SEND == action && type != null) {
                Timber.e("heey $action")
                Timber.e("heey $type")
                val cardDeckUrl = it.getStringExtra(Intent.EXTRA_TEXT)
                Timber.e("heey $cardDeckUrl")
                val cards = DeckExtractorHelper.extractInformationFromURL(cardDeckUrl)
                Timber.e("${cards[0]}")
                Timber.e("${cards[1]}")
                Timber.e("${cards[2]}")
                
                navController.navigate(R.id.walletFragment,Bundle().apply {
                putIntegerArrayList(CARDS_KEY, cards) })
            }
        }
    }

钱包片段

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setHasOptionsMenu(true)
        //TODO figure OUT why arguments are null when passing them from activity to fragment!! 
        arguments?.let {
            it.getIntegerArrayList(MainActivity.CARDS_KEY)?.let { deckListFromGame ->
                viewModel.showDeckFromGame(deckListFromGame)
            }
        }
        initAdapter()
    }

标签: androidandroid-fragmentsandroid-intentandroid-jetpackandroid-jetpack-navigation

解决方案


推荐阅读