3

I have FirstRunActivity and MainActivity (launcher). When app opening and if app opened for the first time, i start FirstRunActivity (with clearing activity history).

It's working ok. But when turning on autorotation in device, then opening app with rotated device, screen is blinking. In log i can see that activity is recreates itself in loop.

Log :

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 code that starting first run activity (in MainActivity.java) with just starting activity, activity is not blinking, but when pressing back button it will open MainActivity with white screen. Or clearing activity history with other way like here: Clear the entire history stack and start a new activity on Android screen is blinking anyway.

So how can i stop blinking and clearing activity history?

EDIT

After tests i have found that in my other activities, that have no connection with above code, have same problem, blinking. When opening activity with rotated device.

Community
  • 1
  • 1
alashow
  • 2,735
  • 3
  • 21
  • 47
  • 1
    Curious as to why almost all of the methods that `FirstRunActivity` overrides, call `super.onStart()` and not their own counterparts? This probably has nothing to do with the problem, but you never know. – Jonny Henly Apr 17 '15 at 18:07
  • On second thought, I noticed that your log is comprised of `FirstRunActivity onStart/onStop/onPause/onResume`, but in reality your log is only comprised of `ActionBarActivity onStart`. So my curiosity might have something more to do with the problem than I thought. I'm still not sure : ) – Jonny Henly Apr 17 '15 at 18:14

2 Answers2

1

Looking at the documentation for IntentCompat.makeRestartActivityTask, it sounds like it creates an intent that serves to restart your current activity. I recommend removing these two lines:

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

I've never needed them while changing activities.

Additionally, the call to mContext.finish() can be accomplished with just finish() and can never be null in that case, most likely the reason that you were seeing the white screen was that mContext was null, so you never successfully finished that activity.

Joey Harwood
  • 961
  • 1
  • 17
  • 27
  • good! now i'm starting activity normally and just finishing mainActivity. weird, but now it's working. i will test this in other activities and then i will accept your question :). btw, i copied mContext.finish() (and some other lines) from another file and forgot to change it. – alashow Apr 17 '15 at 18:32
1

You need to define the Activity in the manifest with a NoDisplay Theme. Then, start FirstRunActivity or LoginActivity (or whatever). The IntentCompat is not needed unless you support versions below HoneyComb, where you can use FLAG_ACTIVITY_CLEAR_TASK

See https://stackoverflow.com/a/4892712/218473

Community
  • 1
  • 1
Maragues
  • 37,861
  • 14
  • 95
  • 96