gpt4 book ai didi

java - onResume 在从其他 Activity 返回时未按预期表现

转载 作者:行者123 更新时间:2023-11-30 11:44:16 25 4
gpt4 key购买 nike

我有一个小应用程序可以计算到指定日期和时间的时间。它运作良好。

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.widget.DigitalClock;
import android.widget.TextView;

public class Day2GoActivity extends Activity {
private TextView tvWeeks, tvDays, tvHours, tvMinutes, tvSeconds, timeUntil;


public Date curDateTime = new Date();
public Calendar today = Calendar.getInstance();
public Calendar leaving = Calendar.getInstance();
protected SimpleDateFormat sdf = new SimpleDateFormat("E dd-MMM-yyyy HH:mm");

protected Handler h = new Handler();
protected Runnable r = new Runnable() {
public void run() {
afficher();
}
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvDays = (TextView) findViewById(R.id.tvDays);
timeUntil = (TextView) findViewById(R.id.timeUntil);
tvWeeks = (TextView) findViewById(R.id.tvWeeks);
tvHours = (TextView) findViewById(R.id.tvHours);
tvMinutes = (TextView) findViewById(R.id.tvMinutes);
tvSeconds = (TextView) findViewById(R.id.tvSeconds);
}

@Override
protected void onResume() {
super.onResume();
Log.i("TAG", "Resuming");
SharedPreferences sharedPreferences = getSharedPreferences("preferences", MODE_PRIVATE);
Log.i("MyActvity", "Year = " + sharedPreferences.getInt("year", Calendar.YEAR));

leaving.set(sharedPreferences.getInt("year", Calendar.YEAR), sharedPreferences.getInt("month", Calendar.MONTH),
sharedPreferences.getInt("dayOfMonth", Calendar.DAY_OF_MONTH), sharedPreferences.getInt("hour", Calendar.HOUR_OF_DAY),
sharedPreferences.getInt("minute", Calendar.MINUTE));
timeUntil.setText("Time until: " + sdf.format(leaving.getTime()));
r.run();
Log.w("TAG", "Resumed");

}

private void afficher() {
today = Calendar.getInstance();
long millisToGo = leaving.getTimeInMillis() - today.getTimeInMillis();
long seconds2go = TimeUnit.MILLISECONDS.toSeconds(millisToGo);
tvSeconds.setText(String.format("%,d seconds", seconds2go));
long minutes2go = TimeUnit.MILLISECONDS.toMinutes(millisToGo);
tvMinutes.setText(String.format("%,d minutes", minutes2go));
long hours2go = TimeUnit.MILLISECONDS.toHours(millisToGo);
tvHours.setText(String.format("%d hours", hours2go));
tvDays.setText(String.format("%d days %d hours", (int) hours2go / 24, hours2go % 24));
long days2go = TimeUnit.MILLISECONDS.toDays(millisToGo);
tvWeeks.setText(String.format("%d weeks %d days", (int) days2go / 7, days2go % 7));
h.postDelayed(r, 1000);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
Intent myIntent = new Intent(getApplicationContext(), editDate.class);
startActivity(myIntent);
return true;
}

但是,当用户更改日期和时间时,我遇到了一个问题。我有第二个 Activity,如您所见,它在 onCreateOptionsMenu 中调用。它也可以正常工作。

import java.util.Calendar;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TimePicker;

public class editDate extends Activity {

protected DatePicker dp1;
protected TimePicker tp1;
protected Calendar c;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit);
dp1 = (DatePicker) findViewById(R.id.datePicker1);
tp1 = (TimePicker) findViewById(R.id.timePicker1);
tp1.setIs24HourView(true);
LoadPreferences();
}

@Override
protected void onDestroy() {
super.onDestroy();
SavePreferences();
}

private void SavePreferences() {
SharedPreferences sharedPreferences = this.getSharedPreferences("preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();

editor.putInt("year", dp1.getYear());
editor.putInt("month", dp1.getMonth());
editor.putInt("dayOfMonth", dp1.getDayOfMonth());
editor.putInt("hour", tp1.getCurrentHour());
editor.putInt("minute", tp1.getCurrentMinute());
editor.commit();
}

private void LoadPreferences() {
SharedPreferences sharedPreferences = this.getSharedPreferences("preferences", MODE_PRIVATE);
c = Calendar.getInstance();

try {
dp1.init(sharedPreferences.getInt("year", c.YEAR), sharedPreferences.getInt("month", c.MONTH),
sharedPreferences.getInt("dayOfMonth", c.DAY_OF_MONTH), null);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
dp1.init(2012, 5, 1, null);
}
tp1.setCurrentHour(sharedPreferences.getInt("hour", c.HOUR_OF_DAY));
tp1.setCurrentMinute(sharedPreferences.getInt("minute", c.MINUTE));
}

}

应用程序运行良好,Log cat 文件中包含所有预期的条目:

06-01 18:21:04.049: I/ApplicationPackageManager(7750): cscCountry is not German : XEU
06-01 18:21:04.079: I/TAG(7750): Resuming
06-01 18:21:04.089: I/MyActvity(7750): Year = 2014
06-01 18:21:04.089: W/TAG(7750): Resumed
06-01 18:21:05.459: W/KeyCharacterMap(7750): No keyboard for id 0
06-01 18:21:05.459: W/KeyCharacterMap(7750): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-01 18:21:05.519: I/ApplicationPackageManager(7750): cscCountry is not German : XEU
06-01 18:21:05.619: W/Picker(7750): beforeTextChanged: 01, 0, 2, 2
06-01 18:21:05.619: W/Picker(7750): onTextChanged: 01
06-01 18:21:05.619: W/Picker(7750): onTextChanged: 01, 0, 2, 2
06-01 18:21:05.619: W/Picker(7750): aftertextchanged: 01
06-01 18:21:05.619: W/Picker(7750): beforeTextChanged: 01, 0, 2, 2
06-01 18:21:05.619: W/Picker(7750): onTextChanged: 01
06-01 18:21:05.619: W/Picker(7750): onTextChanged: 10, 0, 2, 2
06-01 18:21:05.619: W/Picker(7750): aftertextchanged: 10
06-01 18:21:05.619: W/Picker(7750): beforeTextChanged: 2012, 0, 4, 4
06-01 18:21:05.619: W/Picker(7750): onTextChanged: 2012
06-01 18:21:05.619: W/Picker(7750): onTextChanged: 2014, 0, 4, 4
06-01 18:21:05.619: W/Picker(7750): aftertextchanged: 2014
06-01 18:21:08.239: E/DatePicker(7750): adjustMaxDat : 31 10
06-01 18:21:08.239: W/Picker(7750): beforeTextChanged: 10, 0, 2, 2
06-01 18:21:08.249: W/Picker(7750): onTextChanged: 10
06-01 18:21:08.249: W/Picker(7750): onTextChanged: 01, 0, 2, 2
06-01 18:21:08.249: W/Picker(7750): aftertextchanged: 01
06-01 18:21:08.259: W/Picker(7750): beforeTextChanged: 01, 0, 2, 2
06-01 18:21:08.259: W/Picker(7750): onTextChanged: 01
06-01 18:21:08.259: W/Picker(7750): onTextChanged: 10, 0, 2, 2
06-01 18:21:08.259: W/Picker(7750): aftertextchanged: 10
06-01 18:21:08.259: W/Picker(7750): beforeTextChanged: Jun, 0, 3, 3
06-01 18:21:08.259: W/Picker(7750): onTextChanged: Jun
06-01 18:21:08.259: W/Picker(7750): onTextChanged: Jul, 0, 3, 3
06-01 18:21:08.269: W/Picker(7750): aftertextchanged: Jul
06-01 18:21:10.759: I/TAG(7750): Resuming
06-01 18:21:10.759: I/MyActvity(7750): Year = 2014
06-01 18:21:10.759: W/TAG(7750): Resumed

但是它不会更新 TextViewtimeUntil()。我可以相信我在这里做错了什么,但有两个问题。首先 timeUntil()onResume() 启动时更新(在 onCreate() 之后),其次 timeUntil( ),如果您更改屏幕方向,IS 会更新。我知道屏幕重新定位会重新运行 onCreate(),这表明 onResume()onCreate() 之后工作,但不是自己跑的时候。我完全不知所措。

有没有人有什么想法?

根据 Proxy32 的输入(见下文),我已将 onResume void 更改为

@Override
protected void onResume() {
super.onResume();

timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
h.post(new Runnable() {
@Override
public void run() {
Log.i("TAG", "Resuming");
SharedPreferences sharedPreferences = getSharedPreferences("preferences", MODE_PRIVATE);
Log.i("MyActvity", "Year = " + sharedPreferences.getInt("year", Calendar.YEAR));

leaving.set(sharedPreferences.getInt("year", Calendar.YEAR), sharedPreferences.getInt("month", Calendar.MONTH),
sharedPreferences.getInt("dayOfMonth", Calendar.DAY_OF_MONTH),
sharedPreferences.getInt("hour", Calendar.HOUR_OF_DAY), sharedPreferences.getInt("minute", Calendar.MINUTE));
Log.d("TAG", "date " + sdf.format(leaving.getTime()));
timeUntil.setText("Time until: " + sdf.format(leaving.getTime()));
timeUntil.invalidate();
today = Calendar.getInstance();
long millisToGo = leaving.getTimeInMillis() - today.getTimeInMillis();
long seconds2go = TimeUnit.MILLISECONDS.toSeconds(millisToGo);
tvSeconds.setText(String.format("%,d seconds", seconds2go));
long minutes2go = TimeUnit.MILLISECONDS.toMinutes(millisToGo);
tvMinutes.setText(String.format("%,d minutes", minutes2go));
long hours2go = TimeUnit.MILLISECONDS.toHours(millisToGo);
tvHours.setText(String.format("%,d hours", hours2go));
tvDays.setText(String.format("%,d days %d hours", (int) hours2go / 24, hours2go % 24));
long days2go = TimeUnit.MILLISECONDS.toDays(millisToGo);
tvWeeks.setText(String.format("%,d weeks %d days", (int) days2go / 7, days2go % 7));
}
});
}
}, 0, 1000);
Log.w("TAG", "Resumed");

}

我也删除了 afficher。这现在做了我想要它做的。问题解决了。

最佳答案

我不确定我是否理解这个问题,但我会尽力澄清。

onResume 在 onCreate 之后的 onStart(和/或 onRestart)之后被调用。这就是为什么它会在您更改屏幕配置后或应用程序首次启动时按照您的建议运行。

详情请看这里: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

这也意味着当您返回到最初的主 Activity 时,timeUntil 应该被更新。但是,我没有在您的代码中看到您在哪里完成补充 Activity 。另外,“自行运行”是什么意思?

关于java - onResume 在从其他 Activity 返回时未按预期表现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10854148/

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