Activity layout blinking after finish() is called

余生颓废 提交于 2019-12-12 13:09:53

问题


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.


回答1:


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



回答2:


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

}



回答3:


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.




回答4:


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




回答5:


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.




回答6:


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


来源:https://stackoverflow.com/questions/31817937/activity-layout-blinking-after-finish-is-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!