我的应用未保存实例状态(使用onSaveInstanceState()函数)

时间:2019-05-18 23:20:20

标签: java android kotlin

我正在使用Kotlin语言开发一个android应用程序,当我单击另一个选项卡然后返回相同的选项卡时,我需要保存片段状态。

为确保其按预期工作,我插入了一个计数器,每次单击按钮时都会进行计数,如下面的代码所示,但是我的代码似乎有问题,因为每次更改标签时它都会重置为0 (即不保存状态)。

我经历了很多stackoverflow问题和许多视频,但都没有成功。

我在下面的代码中做错了吗?

请感谢您的支持,并在此先感谢您。

MainActivity.kt

class MainActivity : AppCompatActivity() {

private var containerFrame: FrameLayout? = null

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

    containerFrame = findViewById<FrameLayout>(R.id.containerFrame)
    val navigation = findViewById<BottomNavigationView>(R.id.bottomBar)
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectListener)

    val fragment = HomeTabFragment()      //.newInstance()
    addFragment(fragment)
}

private val mOnNavigationItemSelectListener = object : BottomNavigationView.OnNavigationItemSelectedListener {

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.TabHome -> {
                val fragment = HomeTabFragment()      //.newInstance()
                addFragment(fragment)
                return true
            }
            R.id.TabTwo -> {
                val fragment = TabTwoFragment()
                addFragment(fragment)
                return true
            }
            R.id.TabThree -> {
                val fragment = TabThreeFragment()
                addFragment(fragment)
                return true
            }
            R.id.TabFour -> {
                val fragment = TabFourFragment()
                addFragment(fragment)
                return true
            }
        }
        return false
    }
}

private fun addFragment(fragment: Fragment) {
    supportFragmentManager.beginTransaction()
        .setCustomAnimations(R.anim.design_bottom_sheet_slide_in, R.anim.design_bottom_sheet_slide_out)
        .replace(R.id.containerFrame, fragment, fragment.javaClass.simpleName)
        .addToBackStack(fragment.javaClass.simpleName).commit()
}
}

HomeTabFragment.kt

class HomeTabFragment : Fragment() {

var counterOne: Int? = 0

companion object {
    fun newInstance(): HomeTabFragment {
        val homeTabFragment = HomeTabFragment()
        val args = Bundle()
        homeTabFragment.arguments = args
        return homeTabFragment
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    if (savedInstanceState != null) {
        counterOne = savedInstanceState!!.getInt("counter", 0)
    }
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater!!.inflate(R.layout.fragment_tab_home, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    val testButtonOne = view!!.findViewById<Button>(R.id.testButtonOne)
    val resultOne = view!!.findViewById<TextView>(R.id.resultOne)

    resultOne.text = "$counterOne"

    testButtonOne.setOnClickListener {
        counterOne = counterOne!! + 1

        resultOne.text = "$counterOne"
    }
}

override fun onViewStateRestored(savedInstanceState: Bundle?) {
    super.onViewStateRestored(savedInstanceState)
}

override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)

    outState.putInt("counter", counterOne!!)
}
}

activity_main.xml

<LinearLayout 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:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          tools:context=".MainActivity">

<FrameLayout
        android:id="@+id/containerFrame"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

<android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/tabs" />

</LinearLayout>

0 个答案:

没有答案