这里是要下载的项目:https://www.dropbox.com/s/lq3p2pnwjacik7s/testApp.zip?dl=0
我正在使用Android导航组件,这是应用程序的屏幕截图:
如您所见,在MainActivity布局中,有一个nav_host_fragment,一个工具栏和一个底部导航视图。
[Blue Tab的顺序将在图中类似
工具栏“ CUSTOM TITLE HERE”中的标题硬编码在destination_blue1片段中
但是当我从blue1移到blue2,然后使用后退按钮(物理硬件后退按钮)返回到blue1时,标题更改为“ fragment_blue1”,而不是“ CUSTOM TITLE HERE”(硬编码)。
“ fragment_blue1”实际上是该片段的目标图ID的字符串。
我不明白为什么即使我将标题硬编码为“ CUSTOM TITLE HERE”,但如果我使用(硬硬件后退按钮),它就不起作用。
但是如果我使用向上按钮(工具栏/操作栏中的后退按钮),那么根据我硬编码的标题,标题将为“ CUSTOM TITLE HERE”。
这里是我的硬编码标题不起作用时的标题,当我使用后退按钮(物理后退按钮)从blue 2移回blue1时。
这是我实际情况的简化,我需要从片段Blue1动态更改标题。我不明白为什么使用后退按钮后不能更改标题。但是使用向上按钮时,标题将按照我使用的代码显示,与目标ID图不同。
如何解决?出了什么问题?
我正在使用样式<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
,这是我的MainActivity的代码
class MainActivity : AppCompatActivity() {
lateinit var navController : NavController
lateinit var toolbar: Toolbar
lateinit var bottomNavigationView : BottomNavigationView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navController = Navigation.findNavController(this,R.id.nav_host_fragment)
setUpViewDeclaration()
setupBottomNavMenu()
setupActionBar()
}
private fun setUpViewDeclaration() {
toolbar = findViewById(R.id.toolbar)
bottomNavigationView = findViewById(R.id.bottom_nav)
}
private fun setupBottomNavMenu() {
bottom_nav.setupWithNavController(navController)
}
private fun setupActionBar() {
// set up top hierarchy destination
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.destination_blue1,
R.id.destination_red1)
)
setSupportActionBar(toolbar)
toolbar.setupWithNavController(navController,appBarConfiguration)
}
}
这是blue1片段的代码
class Blue1Fragment : Fragment() {
lateinit var moveButton: Button
lateinit var fragmentView: View
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
fragmentView = inflater.inflate(R.layout.fragment_blue1, container, false)
moveButton = fragmentView.findViewById(R.id.move_button_1)
// The title hard coded in here
// this one line below seems doesn't work if using back physical button
(activity as AppCompatActivity).supportActionBar?.title = "CUSTOM TITLE HERE"
moveButton.setOnClickListener {
val nextDestination = Blue1FragmentDirections.actionToBlue2()
Navigation.findNavController(fragmentView).navigate(nextDestination)
}
return fragmentView
}
}
这是blue2片段的代码:
class Blue2Fragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blue2, container, false)
}
}
尝试在“ onViewCreated”中设置标题
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
之所以这样,是因为您添加了片段,而添加了onResume却没有调用。如果您的导航很简单(片段之间的路由始终相同),则可以通过调用SecondFragment中的onStop()来实现它,并在此方法内部将工具栏标题更改为第一个片段标题。如果您的片段动态更改(从fr1调用fr2 / fr3),则可以在bundle中传递标题,并将其替换为自定义标题
//inside Blue2Fragment
override fun onStop() {
(activity as AppCompatActivity).supportActionBar?.title = "CUSTOM TITLE HERE"
super.onStop()
}