gpt4 book ai didi

java - 实例状态未保存

转载 作者:行者123 更新时间:2023-12-02 05:22:57 25 4
gpt4 key购买 nike

我不明白为什么我的包 SavedInstanceState 在应用程序关闭时没有保存。很可能这只是我不知道的愚蠢事情!主要 Activity 没有太多内容,只是一个用于转换屏幕的页面适配器,因为这是我要尝试制作的主要部分。现在大部分代码都在我的 StandFragment.java 中,它是打开应用程序时显示的第一个页面。这就是保存状态之类的地方。

主要 Activity : 公共(public)类 MainActivity 扩展 FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}

private class MyPagerAdapter extends FragmentPagerAdapter {

public MyPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int pos){
switch(pos){
case 0: return StandFragment.newInstance("STAND");
case 1: return CartFragment.newInstance("CART");
case 2: return ThirdFragment.newInstance("THIRD");
case 3: return ThirdFragment.newInstance("FOURTH");
case 4: return ThirdFragment.newInstance("FIFTH");
default: return ThirdFragment.newInstance("Default");
}
}

@Override
public int getCount(){
return 5;
}
}

}

支架 fragment :

      public class StandFragment extends Fragment{

private int totalMoney;
private int moneyPerSec;
private int moneyPerClick;
Handler handleCounter = new Handler();
TextView counter;
TextView perSecCounter;
Button buttonMoney;
Button buttonMoneyPerSec;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.stand_frag, container, false);
super.onCreate(savedInstanceState);

buttonMoney = (Button)v.findViewById(R.id.buttonMoney);
buttonMoneyPerSec = (Button)v.findViewById(R.id.buttonMoneyPerSec);
counter = (TextView)v.findViewById(R.id.tvMoney);
perSecCounter = (TextView)v.findViewById(R.id.tvMonPerSec);

TextView tv = (TextView) v.findViewById(R.id.tvStandFrag);
tv.setText(getArguments().getString("msg"));

handleCounter.post(updateCounter);
if (savedInstanceState==null){
moneyPerSec = 1;
moneyPerClick = 1;
totalMoney=0;
}
else{
moneyPerSec = savedInstanceState.getInt("moneyPerSec");
moneyPerClick = savedInstanceState.getInt("moneyPerClick");
totalMoney = savedInstanceState.getInt("totalMoney");
}

buttonMoney.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
totalMoney += moneyPerClick;
counter.setText(""+totalMoney);
perSecCounter.setText(""+moneyPerSec);
}
});
buttonMoneyPerSec.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if (totalMoney>=10){
totalMoney-=10;
counter.setText(""+totalMoney);
moneyPerSec++;
perSecCounter.setText(""+moneyPerSec);
}
}
});
return v;
}

Runnable updateCounter = new Runnable(){
public void run(){
totalMoney += moneyPerSec;
counter.setText(""+totalMoney);
handleCounter.postDelayed(this, 1000);
}
};
public void onPause(Bundle savedInstanceState){
savedInstanceState.putInt("totalMoney",totalMoney);
savedInstanceState.putInt("moneyPerSec",moneyPerSec);
savedInstanceState.putInt("moneyPerClick",moneyPerClick);
super.onSaveInstanceState(savedInstanceState);

}
public static StandFragment newInstance(String text){

StandFragment f = new StandFragment();
Bundle b = new Bundle();
b.putString("msg", text);

f.setArguments(b);

return f;
}
}

最佳答案

onSaveInstanceState() 不适用于持久存储。它仅用于保存有关 Fragment 实例的特定状态(例如,在配置更改期间,或者您的应用程序在后台被终止并且应重新创建 Fragment)。如果您完全关闭应用程序(即完成 Activity),那么您的状态将不会被保存,因为不再需要该实例。

对于持久存储,您可以根据需要查看 SharedPreferencesSQLite

关于java - 实例状态未保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26346490/

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