Search code examples
androidcrouton

Problems with Infinite Crouton in Android only appears once


I show the crouton using the following code. At first run of the application the Crouton appears. But if I hit back button and reopen the application from launcher or recent list Crouton doesn't appear.

public class MainActivity extends ActionBarActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}


@Override
protected void onResume() {
    super.onResume();
    Style infinite = new Style.Builder().setBackgroundColorValue(
            Style.holoBlueLight).build();

    Configuration configuration = new Configuration.Builder().setDuration(
            Configuration.DURATION_INFINITE).build();

    Crouton crouton = Crouton.makeText(this, "Hello world", infinite);
    crouton.setConfiguration(configuration);
    crouton.show();
} 

}

Showing the crouton either in the onCreate or in onResume doesn't seem to have any effect.


Solution

  • I was able to get it to work as intended by holding a reference to the displayed crouton and in onPause canceling the crouton.

    public class MainActivity extends Activity {
    
        private Crouton mCrouton;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            Style infinite = new Style.Builder().setBackgroundColorValue(
                    Style.holoBlueLight).build();
    
    
            Configuration configuration = new Configuration.Builder().setDuration(
                    Configuration.DURATION_INFINITE).build();
    
            mCrouton = Crouton.makeText(this, "Hello world", infinite);
            mCrouton.setConfiguration(configuration);
            mCrouton.show();
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            mCrouton.cancel();
        }
    }
    

    Also there are other ways using which this issue can be solved.

    • Crouton.clearCroutonsForActivity(this);
    • Crouton.cancelAllCroutons();

    Use what suits your needs.