Android片段未在onStart中调用代码()

时间:2017-03-08 01:10:50

标签: android

我正在创建一个连续的警报应用程序,允许用户在一个“连续警报”中设置多个警报。每个单个警报都存储在ArrayList中。我正在尝试这样做,以便当用户关闭应用程序时,设置的警报的UI将在重新打开应用程序时显示。当android调用onStart()时,它不会在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();
}

1 个答案:

答案 0 :(得分:0)

你提交片段了吗?在您提交后调用它