Blinking operation after function completion ()

When I open my app the Activity starts and inside its onCreate method I check some conditions. If the condition is true, I terminate the current activity and open another. The problem is the first activity flashes on the screen and then the second one opens. Code 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);
        }
    }
}

      

Note that setContentView () after validation, but before starting the second activity, the first is still flashing on the screen. Does anyone know how to make it not blink?

Thanks in advance.

+3


source to share


5 answers


The goal of finish () is to destroy the current activity and remove it from the back stack. By calling the finish line, then firing the intent, you ask for an operation to destroy it yourself (I'm guessing he's trying to rebuild) and then fired the intent in the second. Move finish after startActivity () function



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

      

+6


source


The order of your code is wrong. You don't have to name anything after

finish(); 

      



This is because the action will be destroyed. Anything that follows can lead to 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

}

      

+2


source


instead of doing

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

      

you have to 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 intention is not working as long as onCreate will not be completed, so setContentView

will be called

+1


source


Flickering and flashing activity

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

      

android:noHistory

will clear the activity from the stack that needs to be cleared when pressed again, otherwise it will show activity A on the screen.

+1


source


Perhaps you can separate your condition better to avoid completely setContentView()

when you are planning 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);
    }
}

      

0


source







All Articles