如何从 MainActivity 中的 SettingsListFragment 移动?
<fragment
android:id="@+id/settings_list_fragment"
android:name="com.mandarine.targetList.features.settings.SettingsListFragment"
android:label="@string/settings"
tools:layout="@layout/fragment_settings_list">
</fragment>
因为在我的导航图中我没有来自 MainActivity 的屏幕。我在一项活动中有整个导航,你可以检查代码
class MainActivity : AppCompatActivity(), MainActivityViewContract {
private lateinit var auth: FirebaseAuth
private lateinit var mAuthStateListener: FirebaseAuth.AuthStateListener
private val presenter = MainActivityPresenter(contract = this)
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
signIn()
setupViews()
}
override fun onSupportNavigateUp(): Boolean {
return findNavController(R.id.nav_host_fragment).navigateUp(appBarConfiguration)
}
override fun onResume() {
super.onResume()
auth.addAuthStateListener(mAuthStateListener)
}
override fun onPause() {
super.onPause()
auth.removeAuthStateListener(mAuthStateListener)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
presenter.onActivityResult(requestCode, resultCode)
}
override fun cancelSignIn() {
finish()
}
private fun setupViews() {
val host: NavHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment? ?: return
val navController = host.navController
appBarConfiguration = AppBarConfiguration(navController.graph)
val drawerLayout: DrawerLayout? = findViewById(R.id.drawer_layout)
appBarConfiguration = AppBarConfiguration(
setOf(R.id.target_list, R.id.settings_list_fragment, R.id.calendar_fragment),
drawerLayout
)
setupActionBar(navController, appBarConfiguration)
setupNavigationMenu(navController)
setupBottomNavMenu(navController)
}
private fun setupNavigationMenu(navController: NavController) {
val sideNavView = findViewById<NavigationView>(R.id.nav_view)
sideNavView?.setupWithNavController(navController)
}
private fun setupActionBar(
navController: NavController,
appBarConfig: AppBarConfiguration
) {
setupActionBarWithNavController(navController, appBarConfig)
}
private fun setupBottomNavMenu(navController: NavController) {
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_navigation_view)
bottomNavigationView?.setupWithNavController(navController)
}
private fun signIn() {
auth = FirebaseAuth.getInstance()
mAuthStateListener = FirebaseAuth.AuthStateListener { firebaseAuth ->
presenter.signIn(activity = this, user = firebaseAuth.currentUser)
}
}
}