kotlin android bottom navigation fragment setRetainInstance(true)

岁酱吖の 提交于 2019-12-24 18:52:02

问题


I created an bottom navigation project with 4 fragment and put setHasOptionsMenu(true) in onCreate() of the qponFragment in order to keep the same content of qponFragment after switching around the Fragments. However, it doesn't work, the qponFragment is still refreshed after switching back from other fragments. please help to fix it and find out what is the problem with my code.

Here with the code for 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()
    }
}

Here with the code for 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)

}

回答1:


If you try to keep the fragment instance without creating new Instance everytime, it should work. Please find the code 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()
   }
}

it shows error:



来源:https://stackoverflow.com/questions/50551850/kotlin-android-bottom-navigation-fragment-setretaininstancetrue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!