活动重新创建和闪烁

时间:2015-04-17 17:59:28

标签: java android

我有FirstRunActivity和MainActivity(启动器)。当应用程序打开并且第一次打开应用程序时,我启动FirstRunActivity(具有清除活动历史记录)。

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

记录:

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");
    }
}

MainActivity.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
        }
}

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

那我怎么能停止眨眼和清除活动历史呢?

修改

经过测试,我发现在我的其他活动中,与上面的代码没有关系,有同样的问题,闪烁。使用旋转设备打开活动时。

2 个答案:

答案 0 :(得分:1)

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

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

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

另外,对mContext.finish()的调用只能用finish()来完成,在这种情况下永远不能为null,很可能你看到白屏的原因是mContext为null,所以你永远不会成功完成了这项活动。

答案 1 :(得分:1)

您需要使用NoDisplay主题在清单中定义活动。然后,启动FirstRunActivity或LoginActivity(或其他)。除非您支持HoneyComb以下版本,否则不需要IntentCompat,您可以使用FLAG_ACTIVITY_CLEAR_TASK

请参阅https://stackoverflow.com/a/4892712/218473