我有几个计数器变量,我需要存储他们的值,即使该应用程序关闭或杀死或旋转。我知道saveinstancestate,但我不知道如何使用它。这是我的代码我想保存以下变量值 1.allahuakbarweek 2.allahuakbarmonth 3.subhanallahweek 4.subhanallahmonth 5.alhamdulilahweek 6.alhamdulilahmonth
这是我的代码,它是一个片段
package com.farme5.mosquelocator;
import android.os.Bundle;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
public class FragTasbeeh extends Fragment {
private Spinner spinner;
public static final String[] items={"Alhamdulilah","SubhanAllah","AllahuAkbar"};
ImageView ivIcon;
TextView tvItemName;
TextView mview;
TextView wview;
Button add,reset;
int counter=0;
int week,allahuakbarweek,allahuakbarmonth,subhanallahweek,subhanallahmonth,alhamdulilahweek,alh amdulilahmonth;
int month;
int count,alhamdulilahcount,subhanallahcount,allahuakbarcount;
Time t=new Time(Time.getCurrentTimezone());
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);
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(),android.R.layout.simple_spinner_item, 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;
}
}
private 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+"");
}
});
}
private 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++;
tvItemName.setText(alhamdulilahcount+"");
mview.setText(alhamdulilahmonth+"");
wview.setText(alhamdulilahweek+"");
}
});
}
/* (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);
}
}