2017-03-08 60 views
1

我正在製作一個連續的鬧鐘應用程序,允許用戶在一個單一的「連續鬧鐘」中設置多個鬧鐘。每個單獨的警報都存儲在ArrayList中。我試圖讓它在用戶關閉應用程序時,設置的警報UI在重新打開應用程序時將顯示備份。當android調用onStart()時,它不會在onStart()之後讀取代碼,或者我會知道,因爲我在代碼中有日誌。它出什麼問題了?Android的片段onStart()不調用代碼

@Override 
public void onStart() { 
    super.onStart(); 
    Log.e("asdfasdf", "onStart() called"); 
    SharedPreferences sharedPreferences = getActivity().getSharedPreferences("AlarmDataBase", Context.MODE_PRIVATE); 
    int toLoadSize = sharedPreferences.getInt("arraySize", 0); 
    for (int i = 0; i < toLoadSize; i++) { 
     ConsecAlarm toAdd = new ConsecAlarm(); 
     toAdd.setFromHour(sharedPreferences.getInt(i + "fromHour", 7)); 
     toAdd.setFromMinute(sharedPreferences.getInt(i + "fromMintue", 20)); 
     toAdd.setFromAM(sharedPreferences.getBoolean(i + "fromAM", true)); 
     toAdd.setToHour(sharedPreferences.getInt(i + "toHour", 7)); 
     toAdd.setToMinute(sharedPreferences.getInt(i + "toMinute", 30)); 
     toAdd.setToAM(sharedPreferences.getBoolean(i + "toAM", true)); 
     toAdd.setInterval(sharedPreferences.getInt(i + "interval", 5)); 
     int numAlarms = sharedPreferences.getInt(i + "numAlarms", 3); 
     toAdd.setNumAlarms(numAlarms); 
     for (int j = 0; j < numAlarms; j++) { 
      toAdd.addAlarmHour(sharedPreferences.getInt(i + "alarmHour" + j, 0)); 
      toAdd.addAlarmMin(sharedPreferences.getInt(i + "alarmMin" + j, 0)); 
      Log.e("asdfasdf", toAdd.getAlarmHour(i) + ":" + toAdd.getAlarmMin(i) + " loaded"); 
     } 
     mConsecAlarms.add(toAdd); 
    } 
    updateUI(); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    Log.e("asdfasdf", "onStop() called"); 
    Collections.sort(mConsecAlarms, new Comparator<ConsecAlarm>() { 
     @Override 
     public int compare(ConsecAlarm o1, ConsecAlarm o2) { 
      return o1.getToHour() - o2.getToHour(); 
     } 
    }); 
    SharedPreferences sharedPreferences = getActivity().getSharedPreferences("AlarmDatabase", Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 

    int toSaveSize = mConsecAlarms.size(); 
    editor.putInt("arraySize", toSaveSize); 
    for (int i = 0; i < toSaveSize; i++) { 
     editor.putInt(i + "fromHour", mConsecAlarms.get(i).getFromHour()); 
     editor.putInt(i + "fromMinute", mConsecAlarms.get(i).getFromMinute()); 
     editor.putBoolean(i + "fromAM", mConsecAlarms.get(i).isFromAM()); 
     editor.putInt(i + "toHour", mConsecAlarms.get(i).getToHour()); 
     editor.putInt(i + "toMinute", mConsecAlarms.get(i).getToMinute()); 
     editor.putBoolean(i + "toAM", mConsecAlarms.get(i).isToAM()); 
     editor.putInt(i + "interval", mConsecAlarms.get(i).getInterval()); 
     int numAlarms = mConsecAlarms.get(i).getNumAlarms(); 
     editor.putInt(i + "numAlarms", numAlarms); 
     for (int j = 0; j < numAlarms; j++) { 
      editor.putInt(i + "alarmHour" + j, mConsecAlarms.get(i).getAlarmHour(j)); 
      editor.putInt(i + "alarmMin" + j, mConsecAlarms.get(i).getAlarmMin(j)); 
      Log.e("asdfasdf", mConsecAlarms.get(i).getAlarmHour(j) + ":" + mConsecAlarms.get(i).getAlarmMin(j) + " saved"); 
     } 
    } 
    editor.commit(); 
} 

回答

0

您提交了片段嗎?這是你提交後調用它

+0

請你詳細說明嗎?謝謝 – Dulk