问题:Activity嵌套4个Fragment。假如切换到后台。Activity被杀死。重新进入是否可以记录退出到后台时的页面。
使用: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>
或
/**
* 初始化默认fragment
*
* @param savedInstanceState
*/
private void initDefaultFragment(Bundle savedInstanceState) {
if (savedInstanceState != null) {
LogUtil.e("-------------------------initAgain-------------------------------------->");
//非空校验主要为了防止,置于后台activity被销毁,而fragment没有被销毁,再次add时页面重叠现象
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;
}
}
网友评论