diff --git a/bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/BottomNavigationBar.kt b/bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/BottomNavigationBar.kt index 2b9de75..63d4156 100644 --- a/bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/BottomNavigationBar.kt +++ b/bottom-navigation/src/main/java/com/chenshixin/bottomnavigation/BottomNavigationBar.kt @@ -3,6 +3,8 @@ package com.chenshixin.bottomnavigation import android.content.Context import android.graphics.Color import android.graphics.drawable.Drawable +import android.os.Bundle +import android.os.Parcelable import android.support.design.widget.CoordinatorLayout import android.support.v4.view.ViewCompat import android.support.v4.view.animation.LinearOutSlowInInterpolator @@ -44,6 +46,25 @@ class BottomNavigationBar(context: Context, attrs: AttributeSet) : FrameLayout(c LayoutInflater.from(context).inflate(R.layout.bottom_navigation_bar, this, true) } + override fun onSaveInstanceState(): Parcelable { + val bundle = Bundle() + bundle.putParcelable("instanceState", super.onSaveInstanceState()) + bundle.putInt("mCurrentTab", selectedPosition) + return bundle + } + + override fun onRestoreInstanceState(stateParams: Parcelable) { + var state = stateParams + if (state is Bundle) { + selectedPosition = state.getInt("mCurrentTab") + state = state.getParcelable("instanceState") + if (selectedPosition != 0 && bottom_navigation_bar_item_container.childCount > 0) { + setCurrentTab(selectedPosition) + } + } + super.onRestoreInstanceState(state) + } + fun initialise() { if (items.isEmpty()) { return