leandroBorgesFerreira / LoadingButtonAndroid

A button to substitute the ProgressDialog
MIT License
1.95k stars 214 forks source link

error when startanimation() #5

Closed frankenten13 closed 5 years ago

frankenten13 commented 7 years ago

Beautiful animation. I have a problem when init call mBtLogin.startAnimation(), animation not load full in onCreate. I fix it as put startAnimation in a Handler().postdelay. How to direct call startAnimation() when start app. Thanks.

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       CircularProgressButton mBtLogin = (CircularProgressButton) findViewById(R.id.progress_btn_change_activity);
        mBtLogin.startAnimation();
        /*new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mBtLogin.startAnimation();
            }
        }, 100);*/
}
leandroBorgesFerreira commented 7 years ago

Hi Frankenten13,

This is happening because during the onCreate the button didn't draw it self yet, so it doesn't know the measures that it needs to know before animating...

I can work on a fix to this problem, but first could you explain me a bit better what are you trying to do with the button? Do you need to call the animation with a delay or you need to start your activity the the loading animation?

frankenten13 commented 7 years ago

Thanks for your quick reply, i have a layout Login with funcion auto login when user and password already input, so i need call startAnimation on onCreate. Here my code.

if(!mEtUser.getText().equals("")&& !mEtPassword.getText().equals("")){
            mBtLogin.startAnimation();
        }

Perfect if i can call it on onCreate. Thanks & best regards

leandroBorgesFerreira commented 7 years ago

I will fix that in this next week

mradzinski commented 7 years ago

@leandroBorgesFerreira, sorry to disagree, but this doesn't require much of a fix. onCreate() is not supposed to be used for animating things nor explicit app logic (such as Login) as this can lead to several issues such as lagging due to early fired GC's and others. @frankenten13, you should read about the activity lifecycle and when are you supposed to invoke certain parts of your code. Invoking a Login function during onCreate() is a pretty terrible design decision; you are delaying your activity creation process.

As per Android documentation:

In the onCreate() method, you perform basic application startup logic that should happen only once for the entire life of the activity.

MrStahlfelge commented 6 years ago

When using a ViewModel and LiveData, you set up LiveData Observers in onCreate. So if the Button animation is controlled by the ViewModel, the log in is already started and the device is rotated, the observer will immediately fire from the onCreate method.

leandroBorgesFerreira commented 5 years ago

This problem was already fixed. The button accepts animation commands before the screen get drawn and waits for the right time for the animation to start.