gpt4 book ai didi

java - Android onPause()没有被调用

转载 作者:搜寻专家 更新时间:2023-11-01 08:52:02 24 4
gpt4 key购买 nike

现在我有一个 android fragment ,我想在其中存储持久数据 onPause。但是什么都没有被记录下来。为什么 onPause() 从未在 fragment 中被调用?

fragment :

package com.alarm.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.ToggleButton;

import com.alarm.R;
import com.alarm.models.Alarm;
import com.alarm.sql.Database;

public class AlarmCreateFragment extends Fragment {

ToggleButton sunday;
ToggleButton monday;
ToggleButton tuseday;
ToggleButton wednesday;
ToggleButton thursday;
ToggleButton friday;
ToggleButton saturday;

ToggleButton on;
TimePicker time;
EditText alarmName;
TextView alarmTitle;
private Alarm newAlarm;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
init();
return inflater.inflate(R.layout.frag_create_alarm, container, false);

}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("db", "onCreate called");
}

@Override
public void onPause() {
super.onPause();
Log.e("db", "onPause called");
newAlarm.setHour(getHour());
newAlarm.setMinute(getMinute());
newAlarm.setName(getTitle());
newAlarm.setOn(getOn());
Database db = new Database(getActivity());
db.storeAlarm(newAlarm);
}

private void init() {
sunday = (ToggleButton) getView().findViewById(R.id.sunday);
monday = (ToggleButton) getView().findViewById(R.id.monday);
tuseday = (ToggleButton) getView().findViewById(R.id.tuseday);
wednesday = (ToggleButton) getView().findViewById(R.id.wednesday);
thursday = (ToggleButton) getView().findViewById(R.id.thursday);
friday = (ToggleButton) getView().findViewById(R.id.friday);
saturday = (ToggleButton) getView().findViewById(R.id.saturday);
on = (ToggleButton) getView().findViewById(R.id.alarm_status);
time = (TimePicker) getView().findViewById(R.id.alarm_time);
alarmName = (EditText) getView().findViewById(R.id.edit_alarm_name);
alarmTitle = (TextView) getView().findViewById(R.id.alarm_title);
newAlarm = new Alarm();
}

private int getHour() { // 0-23
return time.getCurrentHour();
}

private int getMinute() {
return time.getCurrentMinute();
}

private String getTitle() {
return alarmTitle.getText().toString();
}

private boolean getOn() {
return on.isChecked();
}

/*
* Toggle the alarm on or off when toggle button clicked
*/
public void changeStatus(View view) {
boolean on = getOn();
if (on) {
newAlarm.setOn(true);
} else {
newAlarm.setOn(false);
}
}

}

Activity :

package com.alarm.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.alarm.R;

public class AlarmCreateActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frag_create_alarm);
}
}

如有任何帮助,我们将不胜感激。我只想知道为什么没有调用 fragment onPause() 方法。 (日志永远不会发生)

最佳答案

试试这个..

fragment onResume()onPause() 仅在 Activity onResume()onPause() 时被调用 被调用。

它们与 Activity 紧密耦合

更多info

关于java - Android onPause()没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22316359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com