Search code examples
javaandroidcallbackandroid-lifecyclecrouton

Android: how to use callback on Crouton onDisplayed()


I would like to start a new activity, but only AFTER my Crouton has been displayed.

I would like to use the onDisplayed() function of the Crouton https://github.com/keyboardsurfer/Crouton/blob/master/library/src/de/keyboardsurfer/android/widget/crouton/LifecycleCallback.java

and then call my activity as follows

Intent i = new Intent(this, MyNewActivity.class);
startActivity(i);
finish();

I've been trying to create my callback function, but with no luck so far...

Thanks!


Solution

  • I solved as follows.

    In my Activity called "TestActivity" I called:

    showCroutonMsgThenGoToActivity(text);
    

    And in the same Class I added these functions:

    public void showCroutonMsgThenGoToActivity(String text) {
        Crouton toShow = null;
    
        LifecycleCallback myCallback = new LifecycleCallback() {
            public void onDisplayed() {
                // do nothing
            }
    
            // Will be called when your {@link Crouton} has been removed.
            public void onRemoved() {
                skipToNextActivity(MyNewActivity.class);
            }
        };
    
    
            //create config 
            Configuration myConfig = new Configuration.Builder().
                    setDuration(1000)  
                    .build();
    
            //create style
            Style myStyle = new Style.Builder().setConfiguration(myConfig)
                    .setBackgroundColor(R.color.green) //check your color!
                    .build();
    
            //apply my custom stile
            toShow = Crouton.makeText(TestActivity.this, text, myStyle);
    
        toShow.setLifecycleCallback(myCallback);
        toShow.show();
    }
    
    
    private void skipToNextActivity(Class c) {
        // go to next activity
        Intent i = new Intent(this, c);
        startActivity(i);
        finish();
    }