Activity layout blinking after finish() is called
Asked Answered
P

7

7

When I open my app, an Activity is started, and inside its onCreate method I'm checking some conditions. If the condition is true, I finish my current Activity and open another one. The problem is: The first activity blinks on the screen and then the second one is opened. The code is below:

public class FirstActivity extends Activity {
    @Override
    protected final void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //some code here...
        checkSomeStuff();
        setContentView(R.layout.my_layout);
        //some code here...
    }
    private void checkSomeStuff() {
        if (/*some condition*/) {
            final Intent intent = new Intent(this, SecondActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            finish();
            startActivity(intent);
        }
    }
}

Notice that setContentView() is after the check, but before the second activity is started, the first one still blinks on the screen. Does anyone know how to make it not blink?

Thanks in advance.

Procyon answered 4/8, 2015 at 19:45 Comment(0)
C
10

The purpose of finish() is to destroy the current activity and remove it from the back stack. By calling finish then firing the intent, you are asking the activity to destroy it self (I assume the blink is it trying to recover) then firing the intent to the second. Move finish to after startActivity()

 @Override
protected final void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //some code here...
    if(checkSomeStuff()) {
         setContentView(R.layout.my_layout);
         //some code here...
    }
}

private boolean checkSomeStuff() {
    if (/*some condition*/) {
        final Intent intent = new Intent(this, SecondActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
        return false;
    }
    return true;
}
Cioban answered 4/8, 2015 at 19:56 Comment(3)
This is the best way I know.Zapata
Hi again, @EthanKeiser I just noticed this solution works fine for Android Lollipop, but when checking in a KitKat device, it still blinks.Procyon
Couple things I noticed.... 1, why is onCreate protected and final? Why is the bundle final? Do you use any polymorphism? Would you please include the entire class, as is?Cioban
J
3

The order of your code is wrong. You should not call anything after

finish(); 

This is because the activity will be destroyed. Anything following could result in strange behavior.

 @Override
  protected final void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //set my layout
    setContentView(R.layout.my_layout);
    //some code here...
    finish();
    //nothing here because activity will be destroyed

}
Josefajosefina answered 4/8, 2015 at 19:50 Comment(0)
M
3

The below-mentioned trick is working perfectly for me. Hope this could useful for others.

Opening activity B from activity A.

To close activity B I am using:

finish(); 
overridePendingTransition(0, 0);

to avoid the black color blinking problem.

Missal answered 12/6, 2018 at 21:18 Comment(0)
S
2

Flicker and blinking Activity

    - Reason finish();
    - Remove finish() in Activity 
    - added android:noHistory="true" in AndroidManifest

The android:noHistory will clear Activity from stack which need to be clear on back press otherwise it shows activity A on screen.

Saadi answered 11/1, 2018 at 8:46 Comment(0)
R
1

instead of doing

checkSomeStuff();
setContentView(R.layout.my_layout);

you should do

private void checkSomeStuff() {
    if (//some condition) {
        final Intent intent = new Intent(this, SecondActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        finish();
        startActivity(intent);
    }else{
        setContentView(R.layout.my_layout);
    }
}

you see the view because the intent does not fire until onCreate is finished so setContentView gets called

Remains answered 4/8, 2015 at 19:50 Comment(1)
Your solution is a variation of the others above, but still works... Thank you!Procyon
D
0

Perhaps you can separate your condition better to entirely avoid setContentView() when you're planning to finish().

public class FirstActivity extends Activity {
    @Override
    protected final void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (someCondition()) {
            goToNextActivity();
        } else {
            setContentView(R.layout.my_layout);
            //some code here...
        }
    }

    private boolean someCondition() {
        /* return result of some condition */
    }

    private void goToNextActivity() {
        final Intent intent = new Intent(this, SecondActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        finish();
        startActivity(intent);
    }
}
Daphene answered 4/8, 2015 at 19:49 Comment(1)
Basically the same solutions as the others above, but still correct Thank you!Procyon
E
0

Try this with your style

<style name="CHTheme" parent="Theme.AppCompat.Light">
    <item name="android:windowDisablePreview">true</item>
</style>
Earl answered 3/7, 2020 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.