問題:Activity嵌套4個Fragment熙卡。假如切換到后臺口柳。Activity被殺死节芥。重新進(jìn)入是否可以記錄退出到后臺時的頁面攒发。
使用:onSaveInstanceState()方法调塌。
@BindView(R.id.fl_content)
FrameLayout flContent;
@BindView(R.id.btn_navigation)
BottomNavigationViewEx btnNavigation;
private BaseFragment[] mFragments = new BaseFragment[4];
private int currentTab = 0;
@Override
protected void onActivityCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
mFragments[0] = TabHomeFragment.newInstance();
mFragments[1] = TabMarketFragment.newInstance();
mFragments[2] = TabTradeFragment.newInstance();
mFragments[3] = MyPageFragment.newInstance();
getSupportDelegate().loadMultipleRootFragment(R.id.fl_content, 0, mFragments);
} else {
currentTab = savedInstanceState.getInt(Constants.CURRENT_TAB);
mFragments[0] = findFragment(TabHomeFragment.class);
mFragments[1] = findFragment(TabMarketFragment.class);
mFragments[2] = findFragment(TabTradeFragment.class);
mFragments[3] = findFragment(TabHomeFragment.class);
showHideFragment(mFragments[currentTab]);
}
btnNavigation.enableAnimation(false);
btnNavigation.enableShiftingMode(false);
btnNavigation.enableItemShiftingMode(false);
btnNavigation.setTypeface(Typeface.DEFAULT_BOLD);
btnNavigation.setOnNavigationItemSelectedListener(item -> {
switch (item.getItemId()) {
case R.id.menu_item_home:
currentTab = 0;
showHideFragment(mFragments[0]);
return true;
case R.id.menu_item_market:
currentTab = 1;
showHideFragment(mFragments[1]);
return true;
case R.id.menu_item_trade:
currentTab = 2;
showHideFragment(mFragments[2]);
return true;
case R.id.menu_item_mine:
currentTab = 3;
showHideFragment(mFragments[3]);
return true;
default:
}
return true;
});
}
@Override
public void setStatusBar() {
StatusBarUtil.setTransparentWithNoNavigation(this, true);
}
@Override
public int getLayoutId() {
return R.layout.activity_main;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(Constants.CURRENT_TAB, currentTab);
}
用到第三方
compile 'com.github.ittianyu:BottomNavigationViewEx:1.2.4'
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<FrameLayout
android:id="@+id/fl_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
android:id="@+id/btn_navigation"
android:layout_width="match_parent"
android:layout_height="@dimen/dp50"
android:background="@color/white"
app:itemIconTint="@color/selector_bottom_item_color"
app:itemTextColor="@color/selector_bottom_item_color"
app:menu="@menu/bottom_navigation_menu" />
</LinearLayout>
或
/**
* 初始化默認(rèn)fragment
*
* @param savedInstanceState
*/
private void initDefaultFragment(Bundle savedInstanceState) {
if (savedInstanceState != null) {
LogUtil.e("-------------------------initAgain-------------------------------------->");
//非空校驗(yàn)主要為了防止惠猿,置于后臺activity被銷毀羔砾,而fragment沒有被銷毀,再次add時頁面重疊現(xiàn)象
currentTab = savedInstanceState.getString(Constants.CURRENT_TAB);
if (!TextUtils.isEmpty(currentTab)) {
switchFragment(currentTab);
LogUtil.e("--------------------------------------------------------------->" + currentTab);
}
} else {
LogUtil.e("-------------------------init------------------------------------->");
TabBuyQCFragment defaultFragment = TabBuyQCFragment.newInstance();
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fl_content, defaultFragment, currentTab)
.commit();
currentFragment = defaultFragment;
}
}