0

I am having few counter variables that I need to store for their values even when that app is closed or kill or rotated. I know about savedinstancestate, but I don't know how to use it. I want to save the following values of the variables:

  1. allahuakbarweek
  2. allahuakbarmonth
  3. subhanallahweek
  4. subhanallahmonth
  5. alhamdulilahweek
  6. alhamdulilahmonth

Here's my code, it is a fragment:

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);
    }
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
Farhan patel
  • 41
  • 1
  • 1
  • 7
  • To save data even after app is killed the simplest approach I guess is to use SharedPreferences instead (atleast for simple types). SavedInstanceState only holds data for the lifetime of your app. Convenient for e.g. orientation changes. – cYrixmorten Jul 13 '14 at 11:49
  • You can see my answer to this question http://stackoverflow.com/questions/19027325/save-and-restore-instance/19027553#19027553 for a rather simple way to use SharedPreferences – cYrixmorten Jul 13 '14 at 11:54
  • can you write me the code to save data while rotation using savedinstancestate – Farhan patel Jul 13 '14 at 11:56
  • The bottom-most example from my link shows just that for a 'answer' string value. The value is saved on pause, and retrieved when Activity is created. – cYrixmorten Jul 13 '14 at 11:59

0 Answers0