android - 从其他应用程序和喷气背包导航接收简单数据
问题描述
这可能是一个已经问过的问题,也可能是一个愚蠢的问题,但我找不到关于我的问题的答案。我正在做一个项目,我使用喷气背包导航组件进行导航,并通过意图过滤器从另一个应用程序接收一些数据。当我调试应用程序时,我看到我在 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()
}
解决方案
推荐阅读
- apache-spark - 在 Spark SQL 中进行交叉连接以进行计算
- laravel - Laravel:获取树父子关系的主要级别类别
- python-3.x - 代码在命令提示符下运行,但不在氢气上
- javascript - 您可以在段落中添加 JavaScript API 单词中的小型大写字母吗
- python - 如何在 excel 中为文件夹中的每个 csv 文件创建一个新工作表
- javascript - 到那时捕获的错误而不是 Promise 的捕获 - React Native
- python - 正则表达式检查字符串是否有任何数字后跟python中的单位并修改它
- python - 在 R 中过滤数值向量(与 Python 作为列表)
- cuda - CUDA 分析工具“没有分析内核”
- python - 如何迭代地多处理成几个numpy数组?