Activity Plays and flashes
I have FirstRunActivity and MainActivity (launcher). When the app opens, and if the app is open for the first time, I launch FirstRunActivity (with a history of clearing activity).
It works fine. But when you enable autorotation in the device, opening the app with the device rotated, the screen flashes. In the log, I can see that the activity is being recreated in a loop.
Magazine:
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
}
}
If I change the code that starts the first launcher activity (in MainActivity.java) with just the start activity only, the activity doesn't blink, but pressing the return button opens the MainActivity with a white screen. Or clear the activity history with another method like here: Clear all history stack and start new activity on Android screen , still blinking.
So how can I stop blinking and clear my activity history?
EDIT
After tests, I found that in my other activities that have no relation to the above code, there is the same problem, blinking. When opening an activity with a rotated device.
source to share
Looking at the documentation for IntentCompat.makeRestartActivityTask it looks like it creates an intent that serves to resume your current activity. I recommend removing these two lines:
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
I never needed them when changing actions.
Also, the call to mContext.finish () can be done with just finish () and can never be null in this case, most likely the reason you saw the white screen was because mContext was null, so that you have never successfully completed this activity.
source to share
You need to define the action in the manifest using the NoDisplay theme. Then start FirstRunActivity or LoginActivity (or whatever). IntentCompat is not needed unless you support versions below HoneyComb where you can use FLAG_ACTIVITY_CLEAR_TASK
source to share