如何使用导航架构组件在另一个 Activity 上显示向上按钮
How to display Up button on another Activity using Navigation Architecture Component
假设我在父级 ActivityA
上,它有一个 Fab 按钮。单击 Fab 按钮后,它会打开 ActivityB
.
ActivityB
包含导航图。所以我设置了导航组件,如:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph())
.build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
现在的问题是,我无法显示转到父级的向上按钮 activity。在 AndroidManifext.xml
上,代码是:
<activity
android:name=".ui.ActivityB"
android:label="Activity B"
android:launchMode="singleTop"
android:parentActivityName=".ui.ActivityA">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.ui.ActivityA" />
</activity>
所以,我的问题是如何显示向上按钮?
@ianhanniballake,上面的是你的 但不是这个:
Toolbar toolbar = findViewById(R.id.toolbar);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder()
.build();
NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration);
或者:
NavigationUI.setupWithNavController(toolbar, navController);
根据 Update UI components with NavigationUI documentation:
By default, the Navigation button is hidden when a user is at a top-level destination of a navigation graph and appears as an Up button in any other destination.
因此,您可以通过传入一组空的顶级目的地,让向上按钮显示在 所有 个目的地上。当您无法在导航图中向上移动时,将调用您通过 setFallbackOnNavigateUpListener
设置的 OnNavigationUpListener
,允许您触发 activity 的向上导航功能:
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder()
.setFallbackOnNavigateUpListener(new AppBarConfiguration.OnNavigateUpListener() {
@Override
public boolean onNavigateUp() {
// Trigger the Activity's navigate up functionality
return super.onSupportNavigateUp();
}
}).build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
// Make sure to call NavigationUI.navigateUp(navController, appBarConfiguration);
// in your Activity's `onSupportNavigateUp()`
下面的方法可以解决你的问题
class SettingsActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_setting)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
val navController: NavController = Navigation.findNavController(this, R.id.nav_host_fragment)
appBarConfiguration = AppBarConfiguration.Builder().build()
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)
fab.setOnClickListener {
navController.navigate(R.id.action_nav_setting_to_nav_notification)
}
}
override fun onSupportNavigateUp(): Boolean {
val navController = Navigation.findNavController(this, R.id.nav_host_fragment)
return NavigationUI.navigateUp(
navController,
appBarConfiguration
) || super.onSupportNavigateUp()
}
}
假设我在父级 ActivityA
上,它有一个 Fab 按钮。单击 Fab 按钮后,它会打开 ActivityB
.
ActivityB
包含导航图。所以我设置了导航组件,如:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph())
.build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
现在的问题是,我无法显示转到父级的向上按钮 activity。在 AndroidManifext.xml
上,代码是:
<activity
android:name=".ui.ActivityB"
android:label="Activity B"
android:launchMode="singleTop"
android:parentActivityName=".ui.ActivityA">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.ui.ActivityA" />
</activity>
所以,我的问题是如何显示向上按钮?
@ianhanniballake,上面的是你的
Toolbar toolbar = findViewById(R.id.toolbar);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder()
.build();
NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration);
或者:
NavigationUI.setupWithNavController(toolbar, navController);
根据 Update UI components with NavigationUI documentation:
By default, the Navigation button is hidden when a user is at a top-level destination of a navigation graph and appears as an Up button in any other destination.
因此,您可以通过传入一组空的顶级目的地,让向上按钮显示在 所有 个目的地上。当您无法在导航图中向上移动时,将调用您通过 setFallbackOnNavigateUpListener
设置的 OnNavigationUpListener
,允许您触发 activity 的向上导航功能:
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder()
.setFallbackOnNavigateUpListener(new AppBarConfiguration.OnNavigateUpListener() {
@Override
public boolean onNavigateUp() {
// Trigger the Activity's navigate up functionality
return super.onSupportNavigateUp();
}
}).build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
// Make sure to call NavigationUI.navigateUp(navController, appBarConfiguration);
// in your Activity's `onSupportNavigateUp()`
下面的方法可以解决你的问题
class SettingsActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_setting)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
val navController: NavController = Navigation.findNavController(this, R.id.nav_host_fragment)
appBarConfiguration = AppBarConfiguration.Builder().build()
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)
fab.setOnClickListener {
navController.navigate(R.id.action_nav_setting_to_nav_notification)
}
}
override fun onSupportNavigateUp(): Boolean {
val navController = Navigation.findNavController(this, R.id.nav_host_fragment)
return NavigationUI.navigateUp(
navController,
appBarConfiguration
) || super.onSupportNavigateUp()
}
}