我正在学习 nav_graph 功能并创建了一个新片段(AddNoteFragment)。在导航设计 UI 中进行编辑后,是的,我可以从 NoteFragment 导航到 AddNoteFragment,并且左上角有一个后退图标(向左箭头)。我认为它是由框架本身处理的,如果我单击按钮,它应该能够导航回来。请参阅下面的屏幕截图。 但实际上当我点击时它没有任何动作。我搜索了类似的问题并尝试覆盖“onOptionsItemSelected”但没有运气。我还没有尝试的一件事是在片段中添加新的工具栏,因为我认为我不必这样做。应该有一种方法可以让当前按钮起作用。它不应该定义默认行为吗?否则显示图标有什么意义?这是常见的行为和要求。
相关代码供您参考。 片段“navigation_note”是三个底部导航选项卡片段之一。您可以看到我添加了用于导航到“addNoteFragment”的操作。
<fragment
android:id="@+id/navigation_note"
android:name="com.guo.ultrasecretary.ui.note.NoteFragment"
android:label="@string/title_note"
tools:layout="@layout/fragment_note" >
<action
android:id="@+id/action_navigation_note_to_addNoteFragment"
app:destination="@id/addNoteFragment" />
</fragment>
addNoteFragment 的java代码:
public class AddNoteFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View addNoteView = inflater.inflate(R.layout.fragment_note_add, container, false);
return addNoteView;
}
//tried but not working
/* @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}*/
}
片段的布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/textInputEditText2"
android:layout_width="293dp"
android:layout_height="94dp"
android:hint="Input note here"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="394dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
我正在使用 Android Studio 4.1 并在 AVD Nexus 6 上运行代码。 请大家帮忙指正。
单击
up button
会触发 onSupportNavigateUp
,如官方文档、中所述
如果在清单中为此活动指定了父项或其活动别名,则将自动处理默认的向上导航。
实现预期行为覆盖
onSupportNavigateUp
而不是 onOptionsItemSelected
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp()
}
编辑:
Up
和Back
按钮用于向上导航层次结构,它们之间的区别是
Back
,系统按钮(左面三角形),用于向上导航层次结构,当位于应用程序的主屏幕/片段中并按返回时,您将被导航到应用程序的out。
Up
,应用栏中向左的箭头,用于向上导航层次结构,但它不会让您离开应用程序。
查看文档了解更多信息
启用后退按钮
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
上班
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
使用按钮的替代方法
public boolean onSupportNavigateUp(){
onBackPressed();
return true;
}
谷歌搜索了一下后,我找到了你的答案。
正如您所说,Fragment 的 actionBar 中有后退按钮。但是,你也说它不起作用。那么,为什么
android studio
实施它?
这是给您的答案。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
...
case android.R.id.home:
switch(currentFragment){
case FRAGMENT1:
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction
.replace(R.id.fragment_container, fragment2);
transaction.commit();
currentFragment = FRAGMENT_2;
return true;
default:
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction
.replace(R.id.fragment_container, fragment1);
transaction.commit();
currentFragment = FRAGMENT_1;
return true;
}
}
我找到了另一个对您有用的链接,但是,它可能与您的问题不相似。
在 Kotlin 中:
setSupportActionBar(yourToolBarId) supportActionBar?.setDisplayHomeAsUpEnabled(true)
覆盖 fun onSupportNavigateUp(): Boolean { onBackPressedDispatcher.onBackPressed() 返回真 }