Android:如何在Crouton onDisplayed()上使用回调

时间:2013-10-04 09:36:30

标签: java android callback android-lifecycle crouton

我想开始一项新活动,但仅在我的Crouton展示之后。

我想使用Crouton的onDisplayed()函数 https://github.com/keyboardsurfer/Crouton/blob/master/library/src/de/keyboardsurfer/android/widget/crouton/LifecycleCallback.java

然后按如下方式调用我的活动

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

我一直在尝试创建我的回调函数,但到目前为止还没有运气......

谢谢!

1 个答案:

答案 0 :(得分:5)

我解决了如下。

在我的名为“TestActivity”的活动中,我打电话给:

showCroutonMsgThenGoToActivity(text);

在同一课程中我添加了这些功能:

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