gpt4 book ai didi

java - 保存和检索 v4 fragment 状态

转载 作者:行者123 更新时间:2023-11-29 23:11:01 25 4
gpt4 key购买 nike

到目前为止,我已经阅读了无数关于保存和检索 fragment 状态的主题。不幸的是,我没有任何效果,Bundle savedInstanceState 总是返回 null。我想做的是实现一个“购物车”,它会记住用户选择的商品。为了实现这一点,我只想保存 Fragment 的一个变量,并在再次调用 Fragment 时检索它。

我不仅想让 fragment 在从后台调用时保持它的状态,而且在从 BottomNavigationView 打开它时也保持它的状态。或者它甚至有什么不同吗?

这是所有 fragment 的父 Activity 类:

public class ShopMainViewScreen extends AppCompatActivity implements ShopFragment.OnFragmentInteractionListener, SearchFragment.OnFragmentInteractionListener, ... {

Fragment mContent;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_layout);

loadFragment(new ShopFragment());
mContent = getSupportFragmentManager().findFragmentById(R.id.fragmentplace);
}

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment;

switch (item.getItemId()) {
case R.id.navigation_home:
fragment = new ShopFragment();
loadFragment(fragment);
return true;
case R.id.navigation_search:
fragment = new SearchFragment();
loadFragment(fragment);
return true;
case R.id.navigation_shoppingCart:
fragment = new CartFragment();
loadFragment(fragment);
return true;
case R.id.navigation_settings:
fragment = new SettingsFragment();
loadFragment(fragment);
return true;
}
return false;
}
};

private boolean loadFragment(Fragment fragment) {
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragmentplace, fragment)
.addToBackStack(null)
.commit();
}
return false;
}

这是包含变量的 fragment (应该存储和检索的mShoppingCart)。

public class CartFragment extends Fragment {

private String mTitle;
private int mQuantity;
ArrayList < String > mShoppingCart;
private OnFragmentInteractionListener mListener;

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
Log.i("onSaveInstanceState", "entered");
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putStringArrayList("CART", mShoppingCart);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
Log.i("onActivityCreated", "entered");
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
Log.i("SavedInstanceState", " not null");
mShoppingCart = savedInstanceState.getStringArrayList("CART");
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
Log.i("onCreate", "entered");
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mTitle = getArguments().getString("PRODUCTTITLE");
mQuantity = getArguments().getInt("QUANTITY");
}
if (savedInstanceState == null) {
Log.i("InstanceState", "is null");
mShoppingCart = new ArrayList < > ();
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
validateCart(mTitle, mQuantity);
return inflater.inflate(R.layout.shoppingcart_fragment_layout, container, false);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString() +
" must implement OnFragmentInteractionListener");
}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}

感谢任何帮助。

最佳答案

savedInstanceState 仅在配置更改期间或应用程序在后台运行并返回到前台时才被水化。要在整个用户 session 期间保存购物车的状态,请考虑使用 view model附加到父 Activity 。如果您希望在多个 session 中保留该数据,您也可以尝试在 SQLite 中保留购物车。

关于java - 保存和检索 v4 fragment 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56022319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com