int counter = 0;
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("value",counter);
}
在您进行定向之后,“ onCreate”方法将被调用,对吗? 所以我们可以这样做
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState == null){
//it is the first time the fragment is being called
counter = 0;
}else{
//not the first time so we will check SavedInstanceState bundle
counter = savedInstanceState.getInt("value",0); //here zero is the default value
}
}