2013-05-04 97 views
0

如何使用onDestroy退出應用程序後保存設置?保存設置onDestroy

例子:

當應用開始,它會開始Main_Activity.class

Button button1; 

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

添加了一個名爲 「Button1的」 按鈕,然後點擊

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); 
      } 
    }); 
} 
時給出一個動作打開新的活動

在Second_Activity.class中添加了2複選框,默認情況下,應用程序啓動checkbox1被選中且checkbox2未被選中。但是,當選中checkbox2並且自動選擇checbox1時,在按下另一個按鈕後,它將啓動Third_Activity.class。

我的問題是我們如何保存這個設置,所以當我們退出應用程序,然後再次啓動應用程序,它會自動啓動Third_Activity.class而不是Main_Activity.class像第一個?

我們應該寫在這部分

protected void onDestroy(){ 
    .................... 
} 
+0

你在調用你自己的'onDestroy()'方法嗎? – FoamyGuy 2013-05-04 16:00:43

回答

1

使用SharedPreferences存儲,這將是您的第一項活動。像以前一樣啓動您的啓動器活動。但在那裏檢查您保存在sharedpreference中的值。所以如果你發現你必須從發射器的第一個開始第三個活動開始第三個活動並完成第一個活動。例如

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 
    } 
    ................ 
} 

在SharedPreferences中,我使用了should_start_third布爾值來檢查第三個活動是否會直接啓動。這是默認的錯誤。

您必須在選中第三個複選框後保存共享首選項的值。以節省下面的使用。

getSharedPreferences(name).edit().putBoolean("should_start_third", true).commit(); 
+0

您能否在這裏寫一些示例代碼?仍然困惑如何使用該 – user2341387 2013-05-04 16:08:55

+0

嗯當然..寫在僞代碼的概念..會沒事嗎? – stinepike 2013-05-04 16:09:58

+0

沒問題就請舉個例子 – user2341387 2013-05-04 16:12:03