为什么后退按钮默认不起作用
why the back button doens't work by default
我正在学习 nav_graph 功能并创建了一个新片段 (AddNoteFragment)。在导航设计UI中编辑后,是的,我可以从NoteFragment导航到AddNoteFragment,左上角有一个后退图标(左向箭头)。我假设它是由框架本身处理的,如果我单击按钮,它应该能够将我导航回来。请参见下面的屏幕截图。
但实际上它在我点击时没有任何动作。我搜索了类似的问题并尝试覆盖“onOptionsItemSelected”但没有运气。我还没有尝试的一件事是在片段中添加新的工具栏,因为我认为我不必这样做。应该有一种方法可以使当前按钮起作用。它不应该定义默认行为吗?否则显示图标是什么意思?这是一个普遍的行为和要求。
相关代码供大家参考。
frangment "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>
java addNoteFragment 代码:
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
,并且如 official docs、
中所述
If a parent was specified in the manifest for this activity or an activity-alias to it, default Up navigation will be handled automatically.
要实现预期的行为覆盖 onSupportNavigateUp
而不是 onOptionsItemSelected
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp()
}
编辑:
Up
和Back
按钮用于向上导航层次结构,它们之间的区别是
Back
,系统按钮(朝左的三角形),用于向上导航层次结构,当在您的应用程序中的主页 screen/fragment 和按返回键时,您将被导航到 应用程序。
Up
,应用栏中向左的箭头,用于向上导航层次结构,但不会让您离开应用。
查看 docs 了解更多信息
启用后退按钮
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
实施它?
这是给你的 the answer。
@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;
}
}
我找到了另一个对你有用的 link,但它可能与你的问题不相似。
我正在学习 nav_graph 功能并创建了一个新片段 (AddNoteFragment)。在导航设计UI中编辑后,是的,我可以从NoteFragment导航到AddNoteFragment,左上角有一个后退图标(左向箭头)。我假设它是由框架本身处理的,如果我单击按钮,它应该能够将我导航回来。请参见下面的屏幕截图。
相关代码供大家参考。 frangment "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>
java addNoteFragment 代码:
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
,并且如 official docs、
If a parent was specified in the manifest for this activity or an activity-alias to it, default Up navigation will be handled automatically.
要实现预期的行为覆盖 onSupportNavigateUp
而不是 onOptionsItemSelected
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp()
}
编辑:
Up
和Back
按钮用于向上导航层次结构,它们之间的区别是
Back
,系统按钮(朝左的三角形),用于向上导航层次结构,当在您的应用程序中的主页 screen/fragment 和按返回键时,您将被导航到 应用程序。Up
,应用栏中向左的箭头,用于向上导航层次结构,但不会让您离开应用。
查看 docs 了解更多信息
启用后退按钮
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
实施它?
这是给你的 the answer。
@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;
}
}
我找到了另一个对你有用的 link,但它可能与你的问题不相似。