我尝试使用this way和this打开设置片段时更改导航图标,但两者都不起作用,我仍然在设置片段中看到汉堡包图标
主要活动
@AndroidEntryPoint
class MainActivity : AppCompatActivity(), HideShowIconInterface {
var APP_START_TIME: Long = 0
private lateinit var appBarConfiguration: AppBarConfiguration
private var _binding: ActivityMainBinding? = null
val binding get() = _binding!!
private lateinit var navController: NavController
private lateinit var postViewModel: PostViewModel
private var _navGraph: NavGraph? = null
private val navGraph get() = _navGraph!!
lateinit var adView: AdView
private var adRequest: AdRequest? = null
private val applicationScope = CoroutineScope(Dispatchers.Unconfined)
lateinit var drawerLayout: DrawerLayout
lateinit var drawerToggle: ActionBarDrawerToggle
override fun onDestroy() {
super.onDestroy()
adView.destroy()
adRequest = null
_binding = null
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
APP_START_TIME = System.currentTimeMillis()
_binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
adView = AdView(this)
delayedInit()
postViewModel = ViewModelProvider(this)[PostViewModel::class.java]
setSupportActionBar(binding.toolbar)
drawerLayout = binding.drawerLayout
drawerToggle = ActionBarDrawerToggle(
this,
drawerLayout,
binding.toolbar,
R.string.app_name,
R.string.app_name
).apply {
drawerLayout.addDrawerListener(this)
this.syncState()
}
initBackStackChangeListener()
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment?
if (navHostFragment != null) {
navController = navHostFragment.navController
}
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.nav_home, R.id.nav_accessory,
R.id.nav_arcade, R.id.nav_fashion,
R.id.nav_food, R.id.nav_heath,
R.id.nav_lifestyle, R.id.nav_sports, R.id.nav_favorites, R.id.settingsFragment
), drawerLayout
)
setupActionBarWithNavController(this, navController, appBarConfiguration)
setupWithNavController(binding.navView, navController)
_navGraph = navController.navInflater.inflate(R.navigation.mobile_navigation)
onBackPressedDispatcher.addCallback(this /* lifecycle owner */,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
// Back is pressed... Finishing the activity
finish()
}
})
}
override fun onResume() {
super.onResume()
adView.resume()
postViewModel.currentDestination.observe(this) { currentDestination ->
Log.w(TAG, "currentDestination: at first run is $currentDestination")
navGraph.setStartDestination(currentDestination)
navController.graph = navGraph
}
navController.addOnDestinationChangedListener { _, destination, _ ->
Log.d(TAG, "addOnDestinationChangedListener: " + destination.id)
if (destination.id != R.id.settingsFragment
&& destination.id != R.id.aboutFragment
&& destination.id != R.id.privacyPolicyFragment
&& destination.id != R.id.termsAndConditionsFragment
) {
postViewModel.saveCurrentDestination(destination.id)
}
}
}
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
override fun showHamburgerIcon() {
supportActionBar?.setDisplayHomeAsUpEnabled(false)
drawerToggle.setDrawerIndicatorEnabled(true)
}
override fun showBackIcon() {
supportActionBar?.setDisplayHomeAsUpEnabled(true)
binding.toolbar.setNavigationIcon(R.drawable.ic_arrow_back_24)
drawerToggle.setDrawerIndicatorEnabled(false)
}
private fun initBackStackChangeListener() {
supportFragmentManager.addOnBackStackChangedListener {
val fragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
if (fragment is SettingsFragment) {
binding.toolbar.setNavigationIcon(R.drawable.ic_arrow_back_24)
drawerToggle.isDrawerIndicatorEnabled = false
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
drawerToggle.setToolbarNavigationClickListener { onBackPressed() }
supportActionBar?.setDisplayHomeAsUpEnabled(true)
} else {
supportActionBar?.setDisplayHomeAsUpEnabled(false)
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
drawerToggle.isDrawerIndicatorEnabled = true
drawerToggle.toolbarNavigationClickListener = null
drawerToggle.syncState()
}
}
}
}
interface HideShowIconInterface {
fun showHamburgerIcon()
fun showBackIcon()
}
设置片段
@AndroidEntryPoint
class SettingsFragment : Fragment() {
private var _binding: FragmentSettingsBinding? = null
private val binding get() = _binding!!
private val settingsViewModel: SettingsViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentSettingsBinding.inflate(inflater, container, false)
(requireActivity() as MainActivity).showBackIcon()
return binding.root
}
1条答案
按热度按时间r7s23pms1#
试试这个
并删除
showHamburgerIcon
和ActionBarDrawerToggle
代码