Issue with Shared Preferecne logic in Android
<p>I am new to Android development and I am developing my first Android app in which I am using the concept of Shared preferences to store the value of a button.</p>

<p><strong>My requirement</strong> :</p>

<p>When the app is opened for the very first time since installation , the user clicks on the button and a timer picker dialog opens. The user selects the preferred time and clicks 'OK' button. After clicking the 'OK' button , the time selected by user is set as text on the button itself. So now the button displays the time set by the user. I want this value to be available whenever the user opens the app apart from first time.</p>

<p>In order to save that button value, I am using shared preference. Hence whenever the user opens the main activity apart from first time, the button displays the value of time selected previously by the user.</p>

<p>On the below code, I have implemented shared preference with the knowledge I have and I tried running my app. When the app is opened for the first time, in the main activity I set the value of time and that time is displayed correctly on the button.</p>

<p><strong>Problem Faced :</strong></p>

<p>When I press the back button(On Stop()) or remove the app from memory by hitting the recent apps button(On Destroy()) and open my app again , the time value on button is available as selected by user previously. Now when I close the app without setting any time and open the app again the button the value displayed previously in is replaced by the default text (i.e. "Set time") .
The problem is when I don't set time and close app , the value in button resets when the app is opened again.</p>

<p>I am not able to understand why the shared preference doesn't work properly from the second time onwards.</p>

<p>Someone please help me in correcting the below code. Thanks in advance :)</p>

<p><strong>Note :</strong> the imports for the class and layout xml file is not included below.</p>

<p><strong>My Code:</strong></p>

<pre><code>    public class MainActivity extends Activity implements OnClickListener {

    SharedPreferences storedata;

    public static final String filename = "StoredData";



    Button ATfrom;

    int ATfromHour;
    int ATfromMinute;

    static long ATfrom_to_millisec;

    String Active_From_Time;

    String SP_Active_From_Time = null;



    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub

        super.onDestroy();
        storedata = getSharedPreferences(filename, 0);
        SharedPreferences.Editor editor = storedata.edit();
        editor.putString("Active_From_Time", Active_From_Time);

        editor.commit();

    }

        @Override
    protected void onStop() {
        // TODO Auto-generated method stub
            super.onStop();
            storedata = getSharedPreferences(filename, 0);
            SharedPreferences.Editor editor = storedata.edit();
            editor.putString("Active_From_Time", Active_From_Time);

            editor.commit();
    } 

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub

        super.onRestart();
        storedata = getSharedPreferences(filename, 0);
        SP_Active_From_Time = storedata.getString("Active_From_Time", "Set time");

        ATfrom.setText(SP_Active_From_Time);

    }
    @Override
    protected void onResume() {

        super.onResume(); 
        storedata = getSharedPreferences(filename, 0);
        SP_Active_From_Time = storedata.getString("Active_From_Time", "Set time");


    ATfrom.setText(SP_Active_From_Time);
    }



    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        storedata = getSharedPreferences(filename, 0);
        SP_Active_From_Time = storedata.getString("Active_From_Time", "Set time");

        ATfrom.setText(SP_Active_From_Time);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        ATfrom = (Button) findViewById(R.id.ATfrom);
        ATfrom.setOnClickListener(this);



        //calling shared preference to get the value of the button ATfrom

        storedata = getSharedPreferences(filename, 0);
        SP_Active_From_Time = storedata.getString("Active_From_Time", "Set time");


        ATfrom.setText(SP_Active_From_Time);

    }

    @Override
    protected void onSaveInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        String statetosave = ATfrom.getText().toString();
        savedInstanceState.putString("saved_state", statetosave);


        super.onSaveInstanceState(savedInstanceState);
    }



    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onRestoreInstanceState(savedInstanceState);
        String statesaved = savedInstanceState.getString("saved_state");
        ATfrom.setText(statesaved);


    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {


        case R.id.ATfrom:

            final Calendar ATfromC = Calendar.getInstance();
            ATfromHour = ATfromC.get(Calendar.HOUR_OF_DAY);
            ATfromMinute = ATfromC.get(Calendar.MINUTE);

            // Launch Time Picker Dialog
            TimePickerDialog ATfromtpd = new TimePickerDialog(MainActivity.this,
                    new TimePickerDialog.OnTimeSetListener() {

                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                            String AM_PM;
                            if (hourOfDay &lt; 12) {
                                AM_PM = "AM";
                            } else {
                                AM_PM = "PM";
                                if (hourOfDay &gt; 12) {
                                    hourOfDay = hourOfDay - 12;
                                }
                            }
                            if (minute &lt; 10) {
                                ATfrom.setText(hourOfDay + ":0" + minute + " " + AM_PM);
                            } else {
                                ATfrom.setText(hourOfDay + ":" + minute + " " + AM_PM);
                            }

                            //Converting time to string for shared preference
                            Active_From_Time = ATfrom.getText().toString();

                            ATfrom_to_millisec = (ATfromHour * 60 * 60 * 1000) + (ATfromMinute * 60 * 1000);


                        }
                    }, ATfromHour, ATfromMinute, false);


            ATfromtpd.show();

            break;
                  }


    }
}
</code></pre>