如何使应用栏上的返回按钮返回到前一个片段,而不是返回到主页活动。

huangapple go评论78阅读模式
英文:

How to make Appbar up button back to previous Fragment instead back to Home Activity

问题

首先,我将给出我的包结构:

├── 主页活动 (Home Activity)
│   ├── 菜单活动 (Menus Activity)
│   │   ├── 菜单片段 (Menus Fragment)
│   │   ├── 网页视图片段 (Web View Fragment)

这是我的菜单活动清单代码

<activity
    android:name=".views.menus.MenusActivity"
    android:exported="false"
    android:parentActivityName=".views.home.HomeActivity"
    android:theme="@style/Theme.AppTheme.NoActionBar">
    <meta-data
        android:name="android.app.lib_name"
        android:value="" />
</activity>

我将主页活动(HomeActivity)设置为菜单活动(MenusActivity)的父活动,并在菜单活动中实现了 setDisplayHomeAsUpEnabled(true)setDisplayShowHomeEnabled(true)。这在按上按钮时能正常工作,我们将被重定向回主页活动(HomeActivity)。

但是有一个问题,所有菜单活动(MenusActivity)的逻辑业务都在菜单片段(MenusFragment)中进行。当在该片段布局上单击项目时,它将将菜单片段的布局替换为网页视图片段(WebViewFragment)的布局。当我想通过点击上按钮返回到菜单片段的布局时,它不会将我重定向到菜单片段,而是返回到主页活动(HomeActivity)。

我已经在我的 WebViewFragment 中实现了 addToBackStack(null),但它只对设备的软返回按钮起作用,不影响应用栏的上按钮。

如何解决这个问题?提前感谢。

英文:

First of all, I'll give my package structure:

├── Home Activity
│   ├── Menus Activity
│   │   ├── Menus Fragment
│   │   ├── Web View Fragment

And there is my Manifest code of my Menus Activity:

<activity
    android:name=".views.menus.MenusActivity"
    android:exported="false"
    android:parentActivityName=".views.home.HomeActivity"
    android:theme="@style/Theme.AppTheme.NoActionBar">
    <meta-data
        android:name="android.app.lib_name"
        android:value="" />
</activity>

I set HomeActivity as a parent activity of my MenusActivity. And implement setDisplayHomeAsUpEnabled(true) and setDisplayShowHomeEnabled(true) into my MenusActivity. Its work properly, when we press the up button, we'll be redirected back to HomeActivity.

But there's the problem, All of my MenusActivity logical business is goes to MenusFragment. And when an item is got clicked on that Fragment layout. It'll replace the MenusFragment layout to WebViewFragment layout. And when I wanna return back to MenusFragment layout, by clicking the Up Button, it doesn't direct me to MenusFragment, instead back to HomeActivity.

I has implement addToBackStack(null) into my WebViewFragment, but its only work on device soft back button, and not affecting the appbar up button.

How to solve this? Thanks in advance.

答案1

得分: 2

在MenusActivity中,覆盖onOptionsItemSelected方法以处理"Up"按钮,如下所示:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    if (item.itemId == android.R.id.home) {
        onBackPressed() // 通过调用onBackPressed()处理"Up"按钮点击
        return true
    }
    return super.onOptionsItemSelected(item)
}

当你从MenusFragment中调用WebViewFragment时,使用以下方式进行addToBackStack(null)

val webViewFragment = WebViewFragment()
val transaction = fragmentManager?.beginTransaction()
transaction?.replace(R.id.fragment_container, webViewFragment)
transaction?.addToBackStack(null) // 将WebViewFragment添加到返回栈中
transaction?.commit()

在你的WebViewFragment中,处理返回按钮的点击以导航,如下所示:

val backButton = rootView.findViewById<Button>(R.id.back_button)
backButton.setOnClickListener {
    activity?.onBackPressed() // 导航回前一个片段
}

希望这对你有帮助。

英文:

In MenusActivity override the `onOptionsItemSelected method to handle the Up button as below

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    if (item.itemId == android.R.id.home) {
        onBackPressed() // Handle Up button click by calling onBackPressed()
        return true
    }
    return super.onOptionsItemSelected(item)
}

When you calling WebViewFragment fragment from MenusFragment make addToBackStack(null) as below

val webViewFragment = WebViewFragment()
val transaction = fragmentManager?.beginTransaction()
transaction?.replace(R.id.fragment_container, webViewFragment)
transaction?.addToBackStack(null) // Add WebViewFragment to the back stack
transaction?.commit()

In your WebViewFragment, handle the back button click to navigate as below

val backButton = rootView.findViewById&lt;Button&gt;(R.id.back_button)
backButton.setOnClickListener {
    activity?.onBackPressed() // Navigate back to the previous fragment
}

I hope this will work for you.

huangapple
  • 本文由 发表于 2023年5月29日 21:55:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定