gpt4 book ai didi

java - 如何限制 fragment 中的后退 Action

转载 作者:行者123 更新时间:2023-11-30 10:19:03 25 4
gpt4 key购买 nike

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

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

我的 Activity :

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
}

还有 Activity 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" />

fragment 示例:

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() 

(支持自:API 级别 5)

然后编写如下代码:

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

这将清除所有 Activity 数据并返回主界面。希望对您有所帮助。

关于java - 如何限制 fragment 中的后退 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48913419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com