我正在尝试使用共享首选项来存储整数值但是当应用程序处于关闭或旋转时,它再次启动为零。 即使我的应用程序被杀死或关闭,我想保存该变量值 我想保存名为 alhamdulilahweek 的变量,这是我的代码
public class FragTasbeeh extends Fragment {
public static final String[] items={"Alhamdulilah","SubhanAllah","AllahuAkbar"};
ImageView ivIcon;
Context context;
TextView tvItemName;
TextView mview;
TextView wview;
Button add,reset;
int counter=0;
int allahuakbarweek,allahuakbarmonth,subhanallahweek,subhanallahmonth,alhamdulilahweek,alhamdulilahmonth;
int month;
int count,alhamdulilahcount,subhanallahcount,allahuakbarcount;
Time t=new Time(Time.getCurrentTimezone());
SharedPreferences share;
public static final String IMAGE_RESOURCE_ID = "iconResourceID";
public static final String ITEM_NAME = "itemName";
FragmentManager frg=getFragmentManager();
Fragment fragment=null;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.tasbeeh,container, false);
if(savedInstanceState==null)
{
alhamdulilahcount=0;
alhamdulilahweek=0;
alhamdulilahmonth=0;
}
else
{
savedInstanceState.putInt("alhamdulilahcount", alhamdulilahcount);
savedInstanceState.putInt("alhamdulilahweek", alhamdulilahweek);
savedInstanceState.putInt("alhamdulilahmonth", alhamdulilahmonth);
}
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
share=getActivity().getSharedPreferences("key", 0);
SharedPreferences.Editor editor= share.edit();
add=(Button)getActivity().findViewById(R.id.button1);
tvItemName=(TextView)getActivity().findViewById(R.id.textView1);
wview=(TextView)getActivity().findViewById(R.id.textView2);
mview=(TextView)getActivity().findViewById(R.id.textView3);
Spinner dropdown = (Spinner)getActivity().findViewById(R.id.spin);
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this.getActivity(),R.layout.actionbar,R.id.spintext, items);
dropdown.setAdapter(adapter);
dropdown.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
switch(position){
case 0:
tvItemName.setText(alhamdulilahcount+"");
mview.setText(alhamdulilahmonth+"");
wview.setText(alhamdulilahweek+"");
alhamdulilahcount();
break;
case 1:
tvItemName.setText(subhanallahcount+"");
mview.setText(subhanallahmonth+"");
wview.setText(subhanallahweek+"");
subhanallahcount();
break;
case 2:
tvItemName.setText(allahuakbarcount+"");
mview.setText(allahuakbarmonth+"");
wview.setText(allahuakbarweek+"");
allahuakbarcount();
break;
default:
break;
}
}
public void allahuakbarcount() {
// TODO Auto-generated method stub
count=0;
month=0;
week=0;
add = (Button)getActivity().findViewById(R.id.button1);
reset = (Button)getActivity().findViewById(R.id.button2);
reset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
allahuakbarcount=0;
tvItemName.setText(allahuakbarcount+"");
}
});
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
allahuakbarcount++;
week++;
month++;
allahuakbarweek++;
allahuakbarmonth++;
tvItemName.setText(allahuakbarcount+"");
mview.setText(allahuakbarmonth+"");
wview.setText(allahuakbarweek+"");
}
});
}
public void subhanallahcount() {
// TODO Auto-generated method stub
count=0;
add = (Button)getActivity().findViewById(R.id.button1);
reset = (Button)getActivity().findViewById(R.id.button2);
reset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
subhanallahcount=0;
tvItemName.setText(subhanallahcount+"");
}
});
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
subhanallahcount++;
week=week+count;
month=month+count;
subhanallahweek++;
subhanallahmonth++;
tvItemName.setText(subhanallahcount+"");
mview.setText(subhanallahmonth+"");
wview.setText(subhanallahweek+"");
}
});
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}); }
public void alhamdulilahcount() {
// TODO Auto-generated method stub
alhamdulilahcount=0;
add = (Button)getActivity().findViewById(R.id.button1);
reset = (Button)getActivity().findViewById(R.id.button2);
reset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
alhamdulilahcount=0;
tvItemName.setText(alhamdulilahcount+"");
}
});
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
alhamdulilahcount++;
week++;
month++;
alhamdulilahweek++;
alhamdulilahmonth++;
SharedPreferences.Editor editor=share.edit();
editor.putInt("value1", alhamdulilahweek);
editor.commit();
int count=share.getInt("value1", -1);
tvItemName.setText(alhamdulilahcount+"");
mview.setText(alhamdulilahmonth+"");
wview.setText(count+"");
}
});
}
/* (non-Javadoc)
* @see android.app.Fragment#onSaveInstanceState(android.os.Bundle)
*/
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("alhamdulilahcount", alhamdulilahcount);
savedInstanceState.putInt("alhamdulilahweek", alhamdulilahweek);
savedInstanceState.putInt("alhamdulilahmonth", alhamdulilahmonth);
}
}