Question

How to save setting after we exit the app using onDestroy?

Example:

When the app start, it will start Main_Activity.class

Button button1;

public class Main_Activity extends Activity {
     super.onCreate(savedInstanceState);
     ................
}

Added a button named "button1" and give an Action to open new activity when clicked

public void button1_newactivity (View v){
     button1 = (Button) findViewById(R.id.button1);
     button1.setOnClickListener (new View.OnClickListener() {

          public void onClick(View arg0) {
               Intent secondactivity=new Intent (getApplicationContext(), Second_Activity.class);
               startActivity(secondactivity);
          }
     });
}

Added 2 Checkbox on Second_Activity.class, as a default when the app start checkbox1 is selected and checkbox2 is not selected. But, when checkbox2 selected and checbox1 automatically not selected, after pressed another button it will start Third_Activity.class.

My Question is how can we save this setting, so when we exit the app, then start the app again, It will automatically start Third_Activity.class not Main_Activity.class like the first one?

What should we write in this part

protected void onDestroy(){
     ....................
}
Was it helpful?

Solution

use SharedPreferences to store which will be your first activity. launch your launcher activity like before. But in there check the value you saved in sharedpreference. So if you find that you have to start 3rd activity from the oncreate of launcher start the third and finish the first one. For example

public class Main_Activity extends Activity {
     super.onCreate(savedInstanceState);
     SharedPreferences pref = getSharedPreferences(name);
     boolean b = pref.getBoolean("should_start_third", false);
     if(b){
        finish();
        start third activity
     }
     ................
}

Here in SharedPreferences i used a should_start_third boolean value to check if the third activity will start directly. this is by default false.

you have to save the value of shared preferences after the third checkbox is selected. to save use like following.

getSharedPreferences(name).edit().putBoolean("should_start_third", true).commit();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top