ban*_*ing 5 android android-architecture-components android-architecture-navigation android-deep-link android-navigation-graph
在我的应用程序中,用户可以选择一个类别,然后在该类别中选择一个项目以最终查看项目详细信息。标准/转发流程是:
SelectCategoryFragment->SelectItemFragment->ViewItemDetailsFragment
在选择类别时,selectedCatId通过Bundlefrom SelectCategoryFragmentto传递SelectItemFragment:
NavController navController = Navigation.findNavController(v);
Bundle args = new Bundle();
args.putLong(SelectItemFragment.ARG_CATEGORY_ID, selectedCatId);
navController.navigate(R.id.action_nav_categories_to_items, args);
Run Code Online (Sandbox Code Playgroud)
SelectItemFragment然后将使用该getArguments().getLong(ARG_CATEGORY_ID)值来查询和显示所选类别中的相应项目。
这很好用。但是我现在正在尝试在用户点击通知时实现深度链接,将他们直接跳转到ViewItemDetailsFragment一个可以将他们带到SelectItemFragment, then的后台堆栈SelectCategoryFragment。
我的问题是,如上所述,SelectItemFragment取决于ARG_CATEGORY_ID传递给它的参数以检索/显示其数据。我已经阅读了深层链接和嵌套导航图,但真的不知道如何通过ARG_CATEGORY_ID深层链接/反向堆栈。
当用户按回时,是否有一种整洁的方式可以将数据从 传递ViewItemDetailsFragment到SelectItemFragment?
TLDR:问题可以使用嵌套图来解决。请参阅本文了解更多详细信息。
让我们定义简单的fragment FragmentRed,FragmentGreen并FragmentBlue用相应的背景颜色来扩展简单布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_dark">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
并声明片段类如下:
class FragmentRed : Fragment() {
private val args: FragmentRedArgs by navArgs()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.red, container, false)
view.findViewById<TextView>(R.id.textView).text = args.foo.toString()
return view
}
}
Run Code Online (Sandbox Code Playgroud)
FragmentGreen和FragmentBlue被复制粘贴,但用相应的颜色文本替换所有颜色文本,即FragmentRedArgs-> FragmentBlueArgs、R.layout.red-> R.layout.blue。
让我们这样声明主要活动布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="@+id/content"
android:layout_height="match_parent">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/main_graph" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
哪里main_graph:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_graph"
app:startDestination="@id/fragment_red">
<fragment
android:id="@+id/fragment_red"
android:name="com.playground.FragmentRed">
<argument
android:name="foo"
android:defaultValue="0"
app:argType="integer" />
<action
android:id="@+id/action_fragment_red_to_fragment_green"
app:destination="@id/fragment_green" />
</fragment>
<navigation
android:id="@+id/secondLevel"
app:startDestination="@id/fragment_green">
<fragment
android:id="@+id/fragment_green"
android:name="com.playground.FragmentGreen">
<argument
android:name="bar"
android:defaultValue="0"
app:argType="integer" />
<action
android:id="@+id/action_fragment_green_to_fragment_blue"
app:destination="@id/fragment_blue" />
</fragment>
<fragment
android:id="@+id/fragment_blue"
android:name="com.playground.FragmentBlue">
<argument
android:name="zar"
android:defaultValue="0"
app:argType="integer" />
</fragment>
</navigation>
</navigation>
Run Code Online (Sandbox Code Playgroud)
现在在里面MainActivity让我们生成一个新的通知,为每个片段传递参数:“foo”(红色)- 1,“bar”(绿色)- 2,“zar”(蓝色)- 3。
我们的期望是单击通知打开带有文本 3 的蓝屏,返回时单击会看到带有 2 的绿屏,再次单击应在屏幕上显示带有 1 的红屏:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navController = findNavController(R.id.nav_host_fragment)
val pendingIntent = navController.createDeepLink()
.setGraph(R.navigation.main_graph)
.setDestination(R.id.fragment_blue)
.setArguments(bundleOf("foo" to 1, "bar" to 2, "zar" to 3))
.createPendingIntent()
createNotificationChannel() // outside of the scope of this answer
val builder = NotificationCompat.Builder(this, "my_channel")
.setContentTitle("title")
.setContentText("content text")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.android)
.setAutoCancel(true)
.setChannelId("channelId")
with(NotificationManagerCompat.from(this)) {
notify(100, builder.build())
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是设备上的实际行为:
| 归档时间: |
|
| 查看次数: |
620 次 |
| 最近记录: |