简体   繁体   中英

(android) how to close an activity after snackbar ended?

i want to close activity after snackbar is over. i have two activties A and B. A activity is open B activity using intent. at B, when snackbar is over in 3 seconds, B is close automatically using finish(). But how to know that snackbar is over? as soon as snackbar was launched, B activity close right away because snackbar is over.

Code in B activity.

Snackbar sb = Snackbar.make(wc, "SAVE", Snackbar.LENGTH_SHORT).setDuration(3000);
sb.show();
finish();

plz help me. Thank you.

Add a callback in your snackbar like this:

sb.addCallback(new Snackbar.Callback() {

    @Override
    public void onDismissed(Snackbar snackbar, int event) {
        if (event == Snackbar.Callback.DISMISS_EVENT_TIMEOUT) {
            // finish your activity here
        }
    }

    @Override
    public void onShown(Snackbar snackbar) {
        ...
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM