gpt4 book ai didi

android - Kotlin Android底部导航 fragment setRetainInstance(true)

转载 作者:行者123 更新时间:2023-12-02 13:38:30 25 4
gpt4 key购买 nike

我创建了一个包含4个片段的底部导航项目,并将setHasOptionsMenu(true)放在qponFragment的onCreate()中,以便在切换Fragments之后保持qponFragment的相同内容。但是,它不起作用,从其他片段切回后,qponFragment仍会刷新。请帮助修复它,并找出我的代码有什么问题。

这里是MainActivity.kt的代码

class MainActivity : AppCompatActivity() {

private var mFirebaseAnalytics: FirebaseAnalytics? = null

private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_qpon -> {
//message.setText(R.string.title_qpon)
actionBarIcon(R.drawable.ic_title_black)
createQponFragment()
return@OnNavigationItemSelectedListener true
}
R.id.navigation_me-> {
//message.setText(R.string.title_me)
actionBarIcon(R.drawable.logged)
createMeFragment()
return@OnNavigationItemSelectedListener true
}
R.id.navigation_tool -> {
//message.setText(R.string.title_tool)
actionBarIcon(R.drawable.logged)
createToolFragment()
return@OnNavigationItemSelectedListener true
}
R.id.navigation_tutorial -> {
//message.setText(R.string.title_tutorial)
actionBarIcon(R.drawable.tutorial)
createTutorialFragment()
return@OnNavigationItemSelectedListener true
}
}
false
}

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

// Obtain the FirebaseAnalytics instance.
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this)

actionBarIcon(R.drawable.ic_title_black)


createQponFragment()
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
}

fun actionBarIcon(imageName:Int) {

setSupportActionBar(findViewById(R.id.my_toolbar))
my_toolbar.setLogo(imageName)

if (imageName == R.drawable.ic_title_black) {
my_toolbar.setTitle("")
}

if (imageName == R.drawable.logged) {

my_toolbar.setTitle("login name")

}
if (imageName == R.drawable.tutorial) {

my_toolbar.setTitle("Tutorial")

}


}

val manager = supportFragmentManager

fun createQponFragment() {
val transaction = manager.beginTransaction()
val fragment = qponFragment()
transaction.replace(R.id.fragmentholder,fragment)
transaction.addToBackStack(null)
transaction.commit()
}

fun createMeFragment() {
val transaction = manager.beginTransaction()
val fragment = meFragment()
transaction.replace(R.id.fragmentholder,fragment)
transaction.addToBackStack(null)
transaction.commit()
}

fun createToolFragment() {
val transaction = manager.beginTransaction()
val fragment = toolFragment()
transaction.replace(R.id.fragmentholder,fragment)
transaction.addToBackStack(null)
transaction.commit()
}

fun createTutorialFragment() {
val transaction = manager.beginTransaction()
val fragment = tutorialFragment()
transaction.replace(R.id.fragmentholder,fragment)
transaction.addToBackStack(null)
transaction.commit()
}
}

这里带有qponFragment.kt的代码
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setRetainInstance(true)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {

setHasOptionsMenu(true)

return inflater.inflate(R.layout.fragment_qpon, container, false)

}

最佳答案

如果您尝试保留片段实例而不是每次都创建新实例,则它应该起作用。请找到代码bel

class MainActivity : AppCompatActivity() {

private var mFirebaseAnalytics: FirebaseAnalytics? = null

private var meFragment:Fragment? = null
var toolFragment :Fragment? =null
var qponFragment:Fragment? =null
var tutorialFragment:Fragment? = null
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_qpon -> {
//message.setText(R.string.title_qpon)
actionBarIcon(R.drawable.ic_title_black)
createQponFragment()
return@OnNavigationItemSelectedListener true
}
R.id.navigation_me-> {
//message.setText(R.string.title_me)
actionBarIcon(R.drawable.logged)
createMeFragment()
return@OnNavigationItemSelectedListener true
}
R.id.navigation_tool -> {
//message.setText(R.string.title_tool)
actionBarIcon(R.drawable.logged)
createToolFragment()
return@OnNavigationItemSelectedListener true
}
R.id.navigation_tutorial -> {
//message.setText(R.string.title_tutorial)
actionBarIcon(R.drawable.tutorial)
createTutorialFragment()
return@OnNavigationItemSelectedListener true
}
}
false
}

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

// Obtain the FirebaseAnalytics instance.
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this)

actionBarIcon(R.drawable.ic_title_black)


createQponFragment()
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
}

fun actionBarIcon(imageName:Int) {

setSupportActionBar(findViewById(R.id.my_toolbar))
my_toolbar.setLogo(imageName)

if (imageName == R.drawable.ic_title_black) {
my_toolbar.setTitle("")
}

if (imageName == R.drawable.logged) {

my_toolbar.setTitle("login name")

}
if (imageName == R.drawable.tutorial) {

my_toolbar.setTitle("Tutorial")

}


}

val manager = supportFragmentManager

fun createQponFragment() {
val transaction = manager.beginTransaction()
if(qponFragment == null) qponFragment = qponFragment() // *****code changed here***********
transaction.replace(R.id.fragmentholder,qponFragment)
transaction.addToBackStack(null)
transaction.commit()
}

fun createMeFragment() {
val transaction = manager.beginTransaction()
if(meFragment == null) meFragment = meFragment()
transaction.replace(R.id.fragmentholder,meFragment)
transaction.addToBackStack(null)
transaction.commit()
}

fun createToolFragment() {
val transaction = manager.beginTransaction()
if(toolFragment == null) toolFragment = toolFragment()
transaction.replace(R.id.fragmentholder,toolFragment)
transaction.addToBackStack(null)
transaction.commit()
}

fun createTutorialFragment() {
val transaction = manager.beginTransaction()
val fragment = tutorialFragment()
if(tutorialFragment == null) toolFragment = tutorialFragment() // *****code changed here***********
transaction.replace(R.id.fragmentholder,tutorialFragment)
transaction.replace(R.id.fragmentholder,fragment)
transaction.addToBackStack(null)
transaction.commit()
}
}

它显示错误:

enter image description here

关于android - Kotlin Android底部导航 fragment setRetainInstance(true),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50551850/

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