gpt4 book ai didi

java - Activity 重现和闪烁

转载 作者:太空狗 更新时间:2023-10-29 13:20:27 25 4
gpt4 key购买 nike

我有 FirstRunActivity 和 MainActivity(启动器)。当应用程序打开时,如果应用程序是第一次打开,我会启动 FirstRunActivity(清除 Activity 历史记录)。

一切正常。但是当在设备中打开自动旋转,然后使用旋转的设备打开应用程序时,屏幕闪烁。在日志中,我可以看到 Activity 在循环中重新创建。

日志:

3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onPause
04-17 22:49:00.390 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStop
04-17 22:49:00.410 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStart
04-17 22:49:00.410 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onResume
04-17 22:49:00.480 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onPause
04-17 22:49:00.480 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStop
04-17 22:49:00.520 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStart
04-17 22:49:00.520 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onResume
04-17 22:49:00.630 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onPause
04-17 22:49:00.630 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStop
04-17 22:49:00.680 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStart
04-17 22:49:00.680 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onResume
04-17 22:49:00.800 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onPause
04-17 22:49:00.810 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStop
04-17 22:49:00.870 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStart
04-17 22:49:00.870 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onResume
04-17 22:49:00.960 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onPause
04-17 22:49:00.970 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStop
04-17 22:49:00.990 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStart
04-17 22:49:00.990 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onResume
04-17 22:49:01.060 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onPause
04-17 22:49:01.060 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStop
04-17 22:49:01.080 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStart
04-17 22:49:01.080 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onResume
04-17 22:49:01.150 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onPause
04-17 22:49:01.150 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStop
04-17 22:49:01.180 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStart
04-17 22:49:01.180 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onResume
04-17 22:49:01.250 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onPause
04-17 22:49:01.250 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStop
04-17 22:49:01.280 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStart
04-17 22:49:01.280 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onResume
04-17 22:49:01.430 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onPause

FirstRunActivity.java :

public class FirstRunActivity extends ActionBarActivity {

@InjectView(R.id.password) EditText passwordView;
@InjectView(R.id.password_retype) EditText passwordRetypeView;
@InjectView(R.id.save) View saveButton;

@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_run);
ButterKnife.inject(this);
}

@Override protected void onStart() {
super.onStart();
U.l("FirstRunActivity onStart");
}

@Override protected void onStop() {
super.onStart();
U.l("FirstRunActivity onStop");
}

@Override protected void onPause() {
super.onStart();
U.l("FirstRunActivity onPause");
}

@Override protected void onResume() {
super.onStart();
U.l("FirstRunActivity onResume");
}
}

主要 Activity .java :

@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
settingsManager = SettingsManager.getInstance(this);
//If app not initialized
if (! settingsManager.isAppInitialized()) {
Intent intent = new Intent(this, FirstRunActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
startActivity(mainIntent);
finish();
} else {
//Other code
}
}

如果我更改启动首次运行 Activity 的代码(在 MainActivity.java 中), Activity 不会闪烁,但当按下后退按钮时,它将打开 MainActivity 并显示白屏。或者用其他方式清除 Activity 历史记录:Clear the entire history stack and start a new activity on Android无论如何屏幕​​都在闪烁。

那么我怎样才能停止闪烁并清除 Activity 历史记录呢?

编辑

测试后我发现在我的其他 Activity 中,与上述代码无关,有同样的问题,闪烁。使用旋转设备打开 Activity 时。

最佳答案

查看 IntentCompat.makeRestartActivityTask 的文档,听起来它创建了一个用于重新启动当前 Activity 的 Intent 。我建议删除这两行:

ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);

我在改变 Activity 时从不需要它们。

此外,对 mContext.finish() 的调用可以只用 finish() 完成,并且在这种情况下永远不能为空,很可能你看到白屏的原因是 mContext 为空,所以你永远不会成功完成了该 Activity 。

关于java - Activity 重现和闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29706359/

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