Я использую навигационный компонент Jetpack (v. 2.2.1), и я не уверен, почему мои переходы между фрагментами не удается оживить. Когда я устанавливаю точки останова внутри хоста nav, опции NavOptions оказываются нулевыми; Я не уверен, что это из-за того, что я сделал не так. Я пытался использовать входы и выходы по умолчанию, а также копировать ресурсы папки @ anim / XML из примера проекта, но безрезультатно.
Что-то, что я должен делать по-другому, когда я инициализирую свой NavigationUI? Или может быть что-то не так с моим XML? Любая помощь очень ценится.
Настройка моего NavigationUI :
/**
* Main activity. The rest of the app is fragments inside a nav graph.
*/
class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
// Location tracking
private val model: HomeSharedViewModel by viewModels()
private lateinit var host: NavHostFragment
private lateinit var drawerLayout: DrawerLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
host = supportFragmentManager.findFragmentById(R.id.my_nav_host_fragment) as NavHostFragment? ?: return
// Set up ActionBar
val navController = host.navController
drawerLayout = findViewById(R.id.drawer_layout)
appBarConfiguration = AppBarConfiguration(setOf(R.id.home_dest, R.id.login_dest), drawerLayout)
setupActionBar(navController, appBarConfiguration)
setupNavigationMenu(navController)
}
private fun setupNavigationMenu(navController: NavController) {
val sideNavView = findViewById<NavigationView>(R.id.nav_view)
sideNavView?.apply {
setupWithNavController(navController)
val menu = this.menu
val signOut = menu.findItem(R.id.sign_out)
signOut.setOnMenuItemClickListener {
onSignOut()
false
}
}
}
private fun setupActionBar(navController: NavController,
appBarConfig : AppBarConfiguration) {
setupActionBarWithNavController(navController, appBarConfig)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val retValue = super.onCreateOptionsMenu(menu)
return retValue
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return item.onNavDestinationSelected(findNavController(R.id.my_nav_host_fragment))
|| super.onOptionsItemSelected(item)
}
override fun onSupportNavigateUp(): Boolean {
return findNavController(R.id.my_nav_host_fragment).navigateUp(appBarConfiguration)
}
private fun onSignIn() {
host.navController.navigate(R.id.login_dest)
}
private fun onSignOut() {
LoginFragment.setLoggedInTo(false)
LoginFragment.clearCredentials()
host.navController.navigate(R.id.login_dest)
drawerLayout.closeDrawer(Gravity.LEFT)
}
}
Мой график навигации:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/home_dest">
<fragment
android:id="@+id/home_dest"
android:name="com.company.app.features.home.HomeFragment"
android:label="Home"
tools:layout="@layout/fragment_home"
>
<action
android:id="@+id/action_home_to_search"
app:destination="@id/search_dest"
app:enterAnim="@anim/slide_in_left"
app:exitAnim="@anim/nav_default_exit_anim"
/>
<action
android:id="@+id/action_home_to_prefs"
app:destination="@id/prefs_dest"
app:enterAnim="@anim/slide_in_left"
app:exitAnim="@anim/nav_default_exit_anim"
/>
<action
android:id="@+id/action_home_to_login"
app:destination="@id/login_dest"
app:enterAnim="@anim/slide_in_left"
app:exitAnim="@anim/nav_default_exit_anim"
/>
<action
android:id="@+id/action_home_to_details"
app:destination="@id/details_dest"
app:popUpTo="@+id/homeFragment"
app:enterAnim="@anim/slide_in_left"
app:exitAnim="@anim/nav_default_exit_anim"
/>
<action
android:id="@+id/action_home_to_report"
app:destination="@id/report_dest"
app:enterAnim="@anim/slide_in_left"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="@+id/search_dest"
android:name="com.company.app.features.search.SearchFragment"
android:label="Search"
tools:layout="@layout/fragment_search" >
<action
android:id="@+id/action_search_to_radio"
app:enterAnim="@anim/nav_default_pop_enter_anim"
app:destination="@id/radio_dest" />
<action
android:id="@+id/action_search_to_query"
app:destination="@id/query_dest"
app:enterAnim="@anim/slide_in_left"
/>
<action
android:id="@+id/action_search_to_results"
app:destination="@id/results_dest"
/>
</fragment>
<fragment
android:id="@+id/report_dest"
android:name="com.company.app.features.report.ReportFragment"
android:label="Report a Sighting"
tools:layout="@layout/fragment_report" >
<action
android:id="@+id/action_report_to_query"
app:destination="@id/query_dest"
app:enterAnim="@anim/slide_in_left"
app:exitAnim="@anim/nav_default_exit_anim"
>
</action>
<action
android:id="@+id/action_report_to_date"
app:destination="@id/date_dest"
app:enterAnim="@anim/nav_default_pop_enter_anim"
/>
<action
android:id="@+id/action_report_to_radio"
app:enterAnim="@anim/nav_default_pop_enter_anim"
app:destination="@id/radio_dest" />
<action
android:id="@+id/action_report_to_map"
app:enterAnim="@anim/nav_default_pop_enter_anim"
app:destination="@id/mappicker_dest" />
</fragment>
<fragment
android:id="@+id/prefs_dest"
android:name="com.company.app.features.prefs.UserPreferenceFragment"
android:label="Preferences" >
</fragment>
<fragment
android:id="@+id/login_dest"
android:name="com.company.app.features.login.LoginFragment"
android:label="Login"
tools:layout="@layout/fragment_welcome_login" >
<action
android:id="@+id/action_login_to_home"
app:destination="@id/home_dest"
app:popUpTo="@+id/nav_graph"
app:popUpToInclusive="true"
app:enterAnim="@anim/slide_in_left"
/>
</fragment>
<dialog
android:id="@+id/details_dest"
android:name="com.company.app.features.details.DetailsFragment"
android:label=""
tools:layout="@layout/fragment_details" >
</dialog>
<fragment
android:id="@+id/filters_dest"
android:name="com.company.app.features.filters.MyFiltersFragment"
android:label="My Filters"
tools:layout="@layout/fragment_manage_filters" >
<action
android:id="@+id/action_filters_dest_to_edit_dest"
app:destination="@id/filter_editor_dest" />
</fragment>
<fragment
android:id="@+id/query_dest"
android:name="com.company.app.features.queries.QueryFragment"
android:label="{dynamicTitle}"
tools:layout="@layout/query_fragment" >
<argument
android:name="dynamicTitle"
app:argType="string"
/>
<argument
android:name="fieldId"
app:argType="integer"
/>
</fragment>
<dialog
android:id="@+id/date_dest"
android:name="com.company.app.features.report.DateTimeFragment"
android:label="fragment_date_picker"
tools:layout="@layout/fragment_date_picker" />
<fragment
android:id="@+id/filter_editor_dest"
android:name="com.company.app.features.filters.edit.FilterEditorFragment"
android:label="{dynamicTitle}"
tools:layout="@layout/fragment_edit_filter" >
<argument
android:name="dynamicTitle"
app:argType="string"
/>
<argument
android:name="isNew"
app:argType="boolean"
android:defaultValue="true" />
</fragment>
<dialog
android:id="@+id/radio_dest"
android:name="com.company.app.features.commonUI.RadioButtonDialog"
android:label="{dynamicTitle}"
tools:layout="@layout/fragment_radio">
<argument
android:name="dynamicTitle"
app:argType="string"
/>
<argument
android:name="fieldId"
app:argType="integer"
/>
<argument
android:name="current"
app:argType="integer"
/>
</dialog>
<fragment
android:id="@+id/mappicker_dest"
android:name="com.company.app.features.report.MapPickerFragment"
android:label="Tap to locate this sighting"
tools:layout="@layout/fragment_map_picker" />
<fragment
android:id="@+id/results_dest"
android:name="com.company.app.features.search.SearchResultsFragment"
android:label="Search Results"
tools:layout="@layout/fragment_searchresult" >
<action
android:id="@+id/action_results_to_details"
app:destination="@id/details_dest" />
</fragment>
</navigation>
Типичный оператор навигации, который не ' Анимация:
private fun onSomethingClicked() {
navigate(HomeFragmentDirections.actionHomeToDetails())
}
private fun navigate(action: NavDirections) {
findNavController().navigate(action) // Should i be passing some extra argument here?
}