Remove up button from action bar when navigating using BottomNavigationView with Android Navigation UI...
I've created a small app that has three fragments for top-level navigation through a BottomNavigationView. If you launch the app and click on a navigation button on the bottom nav, you are presented with an up button in the action bar. Here is the code for the activity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
setSupportActionBar(toolbar)
val navController = navHostFragment.findNavController()
setupActionBarWithNavController(this, navController)
setupWithNavController(bottomNav, navController)
}
override fun onSupportNavigateUp(): Boolean
= findNavController(navHostFragment).navigateUp()
}
Here is a screenshot of the result. The app is launched on the home screen and all I've done is simply click the profile button from the BottomNavigationView.
I've tried listening to the BottomNavigationView's item selections and navigating manually using different NavOptions to no avail. Is there anything we can do to avoid showing an up button in the action bar while the user is navigating with a BottomNavigationView?
android-support-library android-navigation android-jetpack
add a comment |
I've created a small app that has three fragments for top-level navigation through a BottomNavigationView. If you launch the app and click on a navigation button on the bottom nav, you are presented with an up button in the action bar. Here is the code for the activity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
setSupportActionBar(toolbar)
val navController = navHostFragment.findNavController()
setupActionBarWithNavController(this, navController)
setupWithNavController(bottomNav, navController)
}
override fun onSupportNavigateUp(): Boolean
= findNavController(navHostFragment).navigateUp()
}
Here is a screenshot of the result. The app is launched on the home screen and all I've done is simply click the profile button from the BottomNavigationView.
I've tried listening to the BottomNavigationView's item selections and navigating manually using different NavOptions to no avail. Is there anything we can do to avoid showing an up button in the action bar while the user is navigating with a BottomNavigationView?
android-support-library android-navigation android-jetpack
add a comment |
I've created a small app that has three fragments for top-level navigation through a BottomNavigationView. If you launch the app and click on a navigation button on the bottom nav, you are presented with an up button in the action bar. Here is the code for the activity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
setSupportActionBar(toolbar)
val navController = navHostFragment.findNavController()
setupActionBarWithNavController(this, navController)
setupWithNavController(bottomNav, navController)
}
override fun onSupportNavigateUp(): Boolean
= findNavController(navHostFragment).navigateUp()
}
Here is a screenshot of the result. The app is launched on the home screen and all I've done is simply click the profile button from the BottomNavigationView.
I've tried listening to the BottomNavigationView's item selections and navigating manually using different NavOptions to no avail. Is there anything we can do to avoid showing an up button in the action bar while the user is navigating with a BottomNavigationView?
android-support-library android-navigation android-jetpack
I've created a small app that has three fragments for top-level navigation through a BottomNavigationView. If you launch the app and click on a navigation button on the bottom nav, you are presented with an up button in the action bar. Here is the code for the activity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
setSupportActionBar(toolbar)
val navController = navHostFragment.findNavController()
setupActionBarWithNavController(this, navController)
setupWithNavController(bottomNav, navController)
}
override fun onSupportNavigateUp(): Boolean
= findNavController(navHostFragment).navigateUp()
}
Here is a screenshot of the result. The app is launched on the home screen and all I've done is simply click the profile button from the BottomNavigationView.
I've tried listening to the BottomNavigationView's item selections and navigating manually using different NavOptions to no avail. Is there anything we can do to avoid showing an up button in the action bar while the user is navigating with a BottomNavigationView?
android-support-library android-navigation android-jetpack
android-support-library android-navigation android-jetpack
edited Sep 26 '18 at 1:10
jclemus91
1084
1084
asked May 12 '18 at 0:31
jeffmcndjeffmcnd
17311
17311
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Starting with 1.0.0-alpha07 you can use AppBarConfiguration
to configure that behaviour.
AppBarConfiguration
has a Builder constructor so you can reate a new Builder
with a specific set of top level destinations, referenced by their id
(this id
is the one you set on your navigation layout).
Create new AppBarConfiguration
:
val appBarConfiguration = AppBarConfiguration
.Builder(
R.id.navigationHomeFragment,
R.id.navigationListFragment,
R.id.navigationProfileFragment)
.build()
Then, instead of setupActionBarWithNavController(this, navController)
you need to call setupActionBarWithNavController(this, navController, appBarConfiguration)
This is the right way to handle top navigation behaviours.
add a comment |
Use this instead of setupWithNavController
:
navController.addOnNavigatedListener(new NavController.OnNavigatedListener() {
@Override
public void onNavigated(@NonNull NavController controller, @NonNull NavDestination destination) {
toolbar.setTitle(destination.getLabel());
}
});
Or the equivalent in Kotlin.
The only thing setupWithNavController
does is add a listener to change the toolbar title, and creates the up button. If you don't want the up button, just add a listener which changes the toolbar title only.
Thanks Bob, this solved my problem. People like you are why I love Stackoverflow.
– szaske
Nov 6 '18 at 0:19
add a comment |
I had the same problem and I found a way to get the current fragment with the NavController.addOnNavigatedListener, so I applied the following logic within my Activity and for now it works for me
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
val navHost = supportFragmentManager
.findFragmentById(R.id.navHostFragment) as NavHostFragment
navHost.navController.addOnNavigatedListener { _, destination ->
val showButton = showUpButton(destination.id)
//Here occurs the magic
supportActionBar?.setDisplayShowHomeEnabled(showButton)
supportActionBar?.setDisplayHomeAsUpEnabled(showButton)
}
}
//Check according to your convenience which fragment id
//should show or hide the "Up Button"
private fun showUpButton(id: Int): Boolean {
return id != R.id.your_home_fragment && id != R.id.your_list_fragment
&& id != R.id.your_profile_fragment
}
And this is my project...
I do not know if it is the best option but if someone has any better suggestion, it will be welcome
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50301820%2fremove-up-button-from-action-bar-when-navigating-using-bottomnavigationview-with%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Starting with 1.0.0-alpha07 you can use AppBarConfiguration
to configure that behaviour.
AppBarConfiguration
has a Builder constructor so you can reate a new Builder
with a specific set of top level destinations, referenced by their id
(this id
is the one you set on your navigation layout).
Create new AppBarConfiguration
:
val appBarConfiguration = AppBarConfiguration
.Builder(
R.id.navigationHomeFragment,
R.id.navigationListFragment,
R.id.navigationProfileFragment)
.build()
Then, instead of setupActionBarWithNavController(this, navController)
you need to call setupActionBarWithNavController(this, navController, appBarConfiguration)
This is the right way to handle top navigation behaviours.
add a comment |
Starting with 1.0.0-alpha07 you can use AppBarConfiguration
to configure that behaviour.
AppBarConfiguration
has a Builder constructor so you can reate a new Builder
with a specific set of top level destinations, referenced by their id
(this id
is the one you set on your navigation layout).
Create new AppBarConfiguration
:
val appBarConfiguration = AppBarConfiguration
.Builder(
R.id.navigationHomeFragment,
R.id.navigationListFragment,
R.id.navigationProfileFragment)
.build()
Then, instead of setupActionBarWithNavController(this, navController)
you need to call setupActionBarWithNavController(this, navController, appBarConfiguration)
This is the right way to handle top navigation behaviours.
add a comment |
Starting with 1.0.0-alpha07 you can use AppBarConfiguration
to configure that behaviour.
AppBarConfiguration
has a Builder constructor so you can reate a new Builder
with a specific set of top level destinations, referenced by their id
(this id
is the one you set on your navigation layout).
Create new AppBarConfiguration
:
val appBarConfiguration = AppBarConfiguration
.Builder(
R.id.navigationHomeFragment,
R.id.navigationListFragment,
R.id.navigationProfileFragment)
.build()
Then, instead of setupActionBarWithNavController(this, navController)
you need to call setupActionBarWithNavController(this, navController, appBarConfiguration)
This is the right way to handle top navigation behaviours.
Starting with 1.0.0-alpha07 you can use AppBarConfiguration
to configure that behaviour.
AppBarConfiguration
has a Builder constructor so you can reate a new Builder
with a specific set of top level destinations, referenced by their id
(this id
is the one you set on your navigation layout).
Create new AppBarConfiguration
:
val appBarConfiguration = AppBarConfiguration
.Builder(
R.id.navigationHomeFragment,
R.id.navigationListFragment,
R.id.navigationProfileFragment)
.build()
Then, instead of setupActionBarWithNavController(this, navController)
you need to call setupActionBarWithNavController(this, navController, appBarConfiguration)
This is the right way to handle top navigation behaviours.
edited Nov 25 '18 at 13:21
answered Nov 25 '18 at 1:11
Javier CancioJavier Cancio
583413
583413
add a comment |
add a comment |
Use this instead of setupWithNavController
:
navController.addOnNavigatedListener(new NavController.OnNavigatedListener() {
@Override
public void onNavigated(@NonNull NavController controller, @NonNull NavDestination destination) {
toolbar.setTitle(destination.getLabel());
}
});
Or the equivalent in Kotlin.
The only thing setupWithNavController
does is add a listener to change the toolbar title, and creates the up button. If you don't want the up button, just add a listener which changes the toolbar title only.
Thanks Bob, this solved my problem. People like you are why I love Stackoverflow.
– szaske
Nov 6 '18 at 0:19
add a comment |
Use this instead of setupWithNavController
:
navController.addOnNavigatedListener(new NavController.OnNavigatedListener() {
@Override
public void onNavigated(@NonNull NavController controller, @NonNull NavDestination destination) {
toolbar.setTitle(destination.getLabel());
}
});
Or the equivalent in Kotlin.
The only thing setupWithNavController
does is add a listener to change the toolbar title, and creates the up button. If you don't want the up button, just add a listener which changes the toolbar title only.
Thanks Bob, this solved my problem. People like you are why I love Stackoverflow.
– szaske
Nov 6 '18 at 0:19
add a comment |
Use this instead of setupWithNavController
:
navController.addOnNavigatedListener(new NavController.OnNavigatedListener() {
@Override
public void onNavigated(@NonNull NavController controller, @NonNull NavDestination destination) {
toolbar.setTitle(destination.getLabel());
}
});
Or the equivalent in Kotlin.
The only thing setupWithNavController
does is add a listener to change the toolbar title, and creates the up button. If you don't want the up button, just add a listener which changes the toolbar title only.
Use this instead of setupWithNavController
:
navController.addOnNavigatedListener(new NavController.OnNavigatedListener() {
@Override
public void onNavigated(@NonNull NavController controller, @NonNull NavDestination destination) {
toolbar.setTitle(destination.getLabel());
}
});
Or the equivalent in Kotlin.
The only thing setupWithNavController
does is add a listener to change the toolbar title, and creates the up button. If you don't want the up button, just add a listener which changes the toolbar title only.
answered Sep 20 '18 at 18:43
Bob bobbingtonBob bobbington
912910
912910
Thanks Bob, this solved my problem. People like you are why I love Stackoverflow.
– szaske
Nov 6 '18 at 0:19
add a comment |
Thanks Bob, this solved my problem. People like you are why I love Stackoverflow.
– szaske
Nov 6 '18 at 0:19
Thanks Bob, this solved my problem. People like you are why I love Stackoverflow.
– szaske
Nov 6 '18 at 0:19
Thanks Bob, this solved my problem. People like you are why I love Stackoverflow.
– szaske
Nov 6 '18 at 0:19
add a comment |
I had the same problem and I found a way to get the current fragment with the NavController.addOnNavigatedListener, so I applied the following logic within my Activity and for now it works for me
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
val navHost = supportFragmentManager
.findFragmentById(R.id.navHostFragment) as NavHostFragment
navHost.navController.addOnNavigatedListener { _, destination ->
val showButton = showUpButton(destination.id)
//Here occurs the magic
supportActionBar?.setDisplayShowHomeEnabled(showButton)
supportActionBar?.setDisplayHomeAsUpEnabled(showButton)
}
}
//Check according to your convenience which fragment id
//should show or hide the "Up Button"
private fun showUpButton(id: Int): Boolean {
return id != R.id.your_home_fragment && id != R.id.your_list_fragment
&& id != R.id.your_profile_fragment
}
And this is my project...
I do not know if it is the best option but if someone has any better suggestion, it will be welcome
add a comment |
I had the same problem and I found a way to get the current fragment with the NavController.addOnNavigatedListener, so I applied the following logic within my Activity and for now it works for me
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
val navHost = supportFragmentManager
.findFragmentById(R.id.navHostFragment) as NavHostFragment
navHost.navController.addOnNavigatedListener { _, destination ->
val showButton = showUpButton(destination.id)
//Here occurs the magic
supportActionBar?.setDisplayShowHomeEnabled(showButton)
supportActionBar?.setDisplayHomeAsUpEnabled(showButton)
}
}
//Check according to your convenience which fragment id
//should show or hide the "Up Button"
private fun showUpButton(id: Int): Boolean {
return id != R.id.your_home_fragment && id != R.id.your_list_fragment
&& id != R.id.your_profile_fragment
}
And this is my project...
I do not know if it is the best option but if someone has any better suggestion, it will be welcome
add a comment |
I had the same problem and I found a way to get the current fragment with the NavController.addOnNavigatedListener, so I applied the following logic within my Activity and for now it works for me
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
val navHost = supportFragmentManager
.findFragmentById(R.id.navHostFragment) as NavHostFragment
navHost.navController.addOnNavigatedListener { _, destination ->
val showButton = showUpButton(destination.id)
//Here occurs the magic
supportActionBar?.setDisplayShowHomeEnabled(showButton)
supportActionBar?.setDisplayHomeAsUpEnabled(showButton)
}
}
//Check according to your convenience which fragment id
//should show or hide the "Up Button"
private fun showUpButton(id: Int): Boolean {
return id != R.id.your_home_fragment && id != R.id.your_list_fragment
&& id != R.id.your_profile_fragment
}
And this is my project...
I do not know if it is the best option but if someone has any better suggestion, it will be welcome
I had the same problem and I found a way to get the current fragment with the NavController.addOnNavigatedListener, so I applied the following logic within my Activity and for now it works for me
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
val navHost = supportFragmentManager
.findFragmentById(R.id.navHostFragment) as NavHostFragment
navHost.navController.addOnNavigatedListener { _, destination ->
val showButton = showUpButton(destination.id)
//Here occurs the magic
supportActionBar?.setDisplayShowHomeEnabled(showButton)
supportActionBar?.setDisplayHomeAsUpEnabled(showButton)
}
}
//Check according to your convenience which fragment id
//should show or hide the "Up Button"
private fun showUpButton(id: Int): Boolean {
return id != R.id.your_home_fragment && id != R.id.your_list_fragment
&& id != R.id.your_profile_fragment
}
And this is my project...
I do not know if it is the best option but if someone has any better suggestion, it will be welcome
edited Oct 26 '18 at 19:54
answered May 18 '18 at 21:07
jclemus91jclemus91
1084
1084
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50301820%2fremove-up-button-from-action-bar-when-navigating-using-bottomnavigationview-with%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown