使用BottomNavigationView保存片段状态

时间:2017-11-10 09:12:26

标签: android bundle state bottomnavigationview

我正在使用BottomNavigationView。我有三个带表格的标签。我想保留用户在进入另一个标签然后回来时在字段中写的内容。 这是包含BottomNavigationView的活动。

public class MainActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

@BindView(R.id.bottom_navigation)
BottomNavigationView navigation;
@BindView(R.id.toolbar)
Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    initToolbar();
    navigation.setOnNavigationItemSelectedListener(this);
    //select first tab
    navigation.setSelectedItemId(R.id.navigation_expiration_key);
}

@Override
protected void initializeDagger() {
    ((MainApplication) getApplicationContext()).getAppComponent().inject(this);
}

@Override
protected void initializePresenter() {
}

@Override
public int getLayoutId() {
    return R.layout.activity_main;
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    Fragment selectedFragment = null;
    switch (item.getItemId()) {
        case R.id.navigation_expiration_key:
            selectedFragment = KeyExpirationFragment.newInstance();
            break;
        case R.id.navigation_place_identification:
            selectedFragment = LockIdentificationFragment.newInstance();
            break;
        case R.id.navigation_bt_key:
            selectedFragment = BtKeyValidationMenuFragment.newInstance();
            break;
    }
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.main_container, selectedFragment);
    transaction.commit();
    return true;
}

我尝试使用以下方法将状态保存在片段中:

    @Override
public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString(KEY_NUMBER, keyNumberField.getText().toString());
    savedInstanceState.putString(CUSTOMER, customerField.getText().toString());
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
        String keyNumber = savedInstanceState.getString(KEY_NUMBER);
        keyNumberField.setText(keyNumber);
        String customer = savedInstanceState.getString(CUSTOMER);
        customerField.setText(customer);
    }
}

但转到其他Tab时从不调用onSaveInstanceState。 我知道当我们浏览应用程序时,我们不应该保存片段的状态。并且假装在f.e.时被触发。手机旋转。

但是当我们使用ViewPager等保存状态时,应该有一种方法可以正确地保持这些状态。不使用共享首选项或错误修复。

的问候。

1 个答案:

答案 0 :(得分:0)

我自己正在寻找这个答案。我不确定,因为我已经看到了这两种方式,但我确实认为你需要超越其他人的超级线。像这样:

public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {         
     savedInstanceState.putString(KEY_NUMBER, keyNumberField.getText().toString());
     savedInstanceState.putString(CUSTOMER, customerField.getText().toString());
     super.onSaveInstanceState(savedInstanceState);
}

编辑2:我将通过RayWenderlich教程尝试片段。 https://www.raywenderlich.com/169885/android-fragments-tutorial-introduction-2