Java InstanceState未保存
我不明白为什么应用程序关闭时我的捆绑包SavedInstanceState没有保存。很可能这只是我不知道的愚蠢的事情!主活动中没有太多内容,只是一个用于转换屏幕的页面适配器,因为这是我将尝试制作的主要部分。现在大部分代码都在my StandFragment.java中,这是打开应用程序时显示的第一个页面。这就是保存状态和类似的东西 主要活动: 公共类MainActivity扩展了FragmentActivity{Java InstanceState未保存,java,android,Java,Android,我不明白为什么应用程序关闭时我的捆绑包SavedInstanceState没有保存。很可能这只是我不知道的愚蠢的事情!主活动中没有太多内容,只是一个用于转换屏幕的页面适配器,因为这是我将尝试制作的主要部分。现在大部分代码都在my StandFragment.java中,这是打开应用程序时显示的第一个页面。这就是保存状态和类似的东西 主要活动: 公共类MainActivity扩展了FragmentActivity{ @Override protected void onCreate(Bundle
@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;
}
}
}
标准碎片:
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;
}
}
仅当Android系统可能需要重新创建片段的特定实例时,才会调用onSaveInstanceState()
您必须将数据保存在活动的onSaveInstanceState()上onSaveInstanceState()
不用于持久存储。它仅用于保存有关片段实例的特定状态(例如,在配置更改期间,或者如果您的应用程序在后台被杀死,则应重新创建片段)。如果您完全关闭应用程序(即完成活动
),则不会保存您的状态,因为不再需要该实例
对于持久存储,您可以根据需要查看
SharedReferences
或SQLite
好的,谢谢!出于某种原因,我认为保存实例状态会传递变量。还有一个问题,比如说保存一个按钮的状态是否有效?比如它是否可见?是的,但仅适用于片段的实例(因此,如果旋转应用程序,可以保留并保存一个布尔值(按钮的状态),然后在onViewCreated()
中重新应用它)。