Reputation: 1749
I am using bottom navigation
.
In the switched screen, there is also a function to open a dialog fragment
.
I also used navigation for this.
This is because, as soon as this dialog
is finished, data
must be delivered to the screen that opened the dialog.
I used safe args
for this.
But I got the same error as the title.
i know where the error is, but i don't know exactly why it occurred.
According to a search on Stack Overflow, there are people who have had the same problem as me, but it doesn't seem to be an exact solution because the cases are different.
nav_graph
<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/nav_graph"
app:startDestination="@id/calendar">
<fragment
android:id="@+id/calendar"
android:name="com.example.writeweight.fragment.CalendarFragment"
android:label="fragment_calendar"
tools:layout="@layout/fragment_calendar" >
</fragment>
<fragment
android:id="@+id/list"
android:name="com.example.writeweight.fragment.WorkoutListFragment"
android:label="fragment_workout_list"
tools:layout="@layout/fragment_workout_list" />
<fragment
android:id="@+id/write"
android:name="com.example.writeweight.fragment.WritingRoutineFragment"
android:label="WritingRoutineFragment"
tools:layout="@layout/fragment_writing_routine">
<action
android:id="@+id/action_write_to_bodyPartDialog"
app:destination="@id/bodyPartDialog" />
<argument
android:name="title"
app:argType="string"
android:defaultValue="Write" />
</fragment>
<dialog
android:id="@+id/bodyPartDialog"
android:name="com.example.writeweight.fragment.BodyPartDialogFragment"
android:label="BodyPartDialogFragment"
tools:layout="@layout/fragment_body_part_dialog">
<action
android:id="@+id/action_bodyPartDialog_to_write"
app:destination="@id/write">
</action>
</dialog>
</navigation>
MainActivity
class MainActivity : AppCompatActivity() {
private lateinit var bottomNav: BottomNavigationView
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
bottomNav = findViewById(R.id.bottom_nav)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_container) as NavHostFragment
navController = navHostFragment.navController
bottomNav.setupWithNavController(navController)
}
}
DialogFragment
class BodyPartDialogFragment : DialogFragment() {
private lateinit var ll: LinearLayout
private lateinit var startBtn: Button
private lateinit var title: String
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.fragment_body_part_dialog, container, false)
initView(view)
setLayout()
startBtn?.setOnClickListener { v ->
title = BodyPartCustomView.getTitle()
val action = BodyPartDialogFragmentDirections.actionBodyPartDialogToWrite(title)
findNavController()?.navigate(action) // Possibly the location of the error.
dismiss()
}
return view
}
private fun initView(view: View) {
ll = view.findViewById(R.id.ll_body_part)
startBtn = view.findViewById(R.id.start)
}
private fun setLayout() {
ll?.apply { clipToOutline = true }
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog:Dialog = super.onCreateDialog(savedInstanceState)
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog.setCanceledOnTouchOutside(false)
return dialog
}
override fun onDismiss(dialog: DialogInterface) {
Toast.makeText(context, "CHECK", Toast.LENGTH_SHORT).show()
}
}
Upvotes: 4
Views: 8258
Reputation: 1
This is how I fixed, I cange the FragmentDirections action for the destination Id ej : navController.navigate(R.id.MainNavigation)
.
In my case, I have two navigations graph and I was trying to navigate between graph. I hope this would help someone
Upvotes: 0
Reputation: 11
maybe you can just make sure id in fragment navigation is same with id in menu button because i already experienced it
Upvotes: 0
Reputation: 564
It can be resolved by checking the current destination first before navigating
For example
Fragments A, B, and C
navigating from A to B while clicking on a button in fragment A that navigates to C might lead to crashes in some cases
for that you should check the current destination first as follows:
if(findNavController().currentDestination?.id==R.id.AFragment)
findNavController().navigate(
AFragmentDirections.actionAFragmentToCFragment()
)
Upvotes: 5