MobileDev
MobileDev

Reputation: 165

Snackbar doesn't react for snackbar.dismiss()

I have a problem with snackbar. My snackbar doesn't react for dismiss

My code:

ConnectivityReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager cm =
                    (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

            Snackbar snackbar = Snackbar
                    .make(drawer, getResources().getString(R.string.no_internet), Snackbar.LENGTH_INDEFINITE)
                    .setAction(getResources().getString(R.string.settings), new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
                        }
                    });

            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            boolean isConnected = activeNetwork != null &&
                    activeNetwork.isConnectedOrConnecting();

            if(!isConnected){
                snackbar.show();
            }else {
                snackbar.dismiss();
            }
        }
    };

I checked and my app is in else state if isConnected return true. Thanks for help.

Upvotes: 4

Views: 2315

Answers (1)

Yann Huissoud
Yann Huissoud

Reputation: 1033

You try to dismiss a snackbar that is not showing, because you don't have a reference to the one that you've created earlier. Move the SnackBar declaration as a field of your class and it should work.

Upvotes: 5

Related Questions