【问题标题】:How to set limit to back action in fragment如何在片段中设置支持动作的限制
【发布时间】:2018-02-21 18:55:11
【问题描述】:

我有 3 个不同的片段。这是一个奇怪的问题,但我想将交易限制在一些片段中。例如:如果我在第三个片段中单击返回,我会回到第一个。如果我在第一个中单击返回,它将返回到 MainActivity。但所有三个片段都是来自另一个名为 SearchActivity 的 Activity。这种方式有两个问题。 第一:我的状态栏看起来有点……看看吧: look at this status bar

我的第二个问题:当我返回片段时(从 MainActivity 开始 SearchActivity,底部导航栏和 currentItem(0),然后我按下返回按钮,我认为它会进入 SearchActivity,但我需要回到 MainActivity。好吧,要回到 MainActivity,我需要按两次,因为第一次按它会进入 SearchActivity)会发生这种情况: imgur gif

我的活动:

lateinit var toolbar: ActionBar

private fun openFragment(fragment: Fragment) {
    val transaction = supportFragmentManager.beginTransaction()
    transaction.replace(R.id.container, fragment)
    transaction.addToBackStack(null)
    transaction.commit()
}

private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
    when (item.itemId) {
        R.id.navigation_1 -> {
            toolbar.title = resources.getString(R.string.title_1)
            val firstFragment = FirstView.newInstance()
            openFragment(firstFragment)
            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_2-> {
            toolbar.title = resources.getString(R.string.title_2)
            val secFragment = SecView.newInstance()
            openFragment(secFragment)
            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_3 -> {
            toolbar.title = resources.getString(R.string.title_3)
            val thirdFragment = ThirdView.newInstance()
            openFragment(thirdFragment)
            return@OnNavigationItemSelectedListener true
        }
    }
    false
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_search)

    toolbar = supportActionBar!!

    val bottomNavigation: BottomNavigationView = findViewById(R.id.navigation)
    bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
    bottomNavigation.selectedItemId = R.id.navigation_1
}

还有活动xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="s.com.s.SearchActivity">

<!--<FrameLayout-->
    <!--android:layout_width="wrap_content"-->
    <!--android:layout_height="wrap_content"/>-->

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="@android:color/white"
    app:itemBackground="@android:color/white"
    app:itemIconTint="@drawable/tab_state"
    app:itemTextColor="@drawable/tab_state"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation" />

片段示例:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view = inflater.inflate(R.layout.fragment_first, container, false)

    val rv = view.findViewById<RecyclerView>(R.id.firstView)
    rv.layoutManager = LinearLayoutManager(context, LinearLayout.VERTICAL, false)
    val firsts = ArrayList<Hotel>()
    firsts.add(Hotel("Paul", "Mr", R.drawable.abc_list_longpressed_holo))
    firsts.add(Hotel("Jane", "Miss", R.drawable.abc_ratingbar_material))
    firsts.add(Hotel("John", "Dr", R.drawable.abc_btn_check_to_on_mtrl_000))
    firsts.add(Hotel("Amy", "Mrs", R.drawable.abc_ic_menu_paste_mtrl_am_alpha))

    var adapter = CustomAdapter(firsts)
    rv.adapter = adapter

    return view
}

companion object {
    fun newInstance(): FirstView = FirstView()
}

还有xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="?attr/actionBarSize"
    android:id="@+id/firstView"/>

很高兴得到帮助,我一个星期都解决不了。我希望我已经解释清楚了,因为我什至不知道如何解释。

【问题讨论】:

  • 您需要跟踪当前片段并覆盖 onBackPressed。然后你会在 Activity 的 onBackPressed 中添加一些逻辑,根据你所在的 Fragment 来决定要做什么。
  • 你有什么例子吗?我只是不明白如何处理片段(只听说过活动),所以......希望你能提供帮助......

标签: java android user-interface android-fragments kotlin


【解决方案1】:

我有一个可能的解决方案:

你应该需要实现

onBackPressed() 

(支持自:API 级别 5)

然后编写如下代码:

  Intent i=new Intent(this, MainActivity.class);
  i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  startActivity(i);

这将清除所有活动数据并返回主目录。 希望对你有帮助。

【讨论】:

  • 我稍后试试,谢谢。但是,对不起,我在这方面不是很聪明。当我需要将片段切换到另一个片段时(例如,从 fr3 到 fr1 而没有其他地方),如何解决这个问题?如果你知道如何解决第一个问题 - 我很高兴你。无论如何,我会记住的
  • 别担心,首先.. 1. 你想要一个状态栏? (我不知道你是否想要,但你可以删除它)2.“我想将事务限制在一些片段中。例如:如果我在第三个片段中点击返回,我会回到第一个。如果我点击回到第一个,它会回到 MainActivity。”如果您想跳过“搜索活动”并直接进入主活动,则需要使用“onBackPressed”功能并指定“当您按下手机的后退按钮时”您将清除片段并启动主活动。我明白这就是你想要的。更多问题回复:)
  • 非常感谢您提供如此丰富的答案。它帮助了我。没想到这么容易解决。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多