Ask Your Question
3

After updating from targetSdkVersion 30 to 33, why does the Action Bar title and menu disappear when replacing a fragment?

asked 2023-05-24 08:58:17 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-24 09:28:01 +0000

lalupa gravatar image

The behavior of the Action Bar (also known as the Toolbar) has changed in more recent versions of Android. In earlier versions, the Action Bar was part of the Activity layout, and replacing a fragment did not affect the Action Bar. However, in more recent versions, the Action Bar is tied to the current Fragment, and changing fragments can result in the Action Bar being replaced as well.

To fix this issue, you need to make sure that each Fragment has its own Action Bar or Toolbar. Use setHasOptionsMenu(true) in each Fragment to specify that it should have its own menu. Also, make sure that you are inflating the correct menu in each Fragment's onCreateOptionsMenu() method. Finally, if you are using Navigation Components, consider using the Toolbar provided by the Navigation Component to ensure consistent behavior across fragments.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-24 08:58:17 +0000

Seen: 20 times

Last updated: May 24 '23