简体   繁体   中英

Show snackbar after startactivity

I created a sign-out button in my app.

Once the user clicks sign out, it signs out and moves the log-in activity.

I had like that once the user clicks on sign-out, it will move to the log-in page and then show the snackbar saying "you have been signed out successfully".

How can I make the snackbar appear only after it moved to the new activity? Because right now it won't show since it changes the activity.

public void signOut(){

    auth.signOut();

    AuthUI.getInstance()
            .signOut(this)
            .addOnCompleteListener( task -> {
                Intent intent = new Intent(ChangePassActivity.this, SignInActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                        Intent.FLAG_ACTIVITY_CLEAR_TASK |
                        Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);

                Pop_Snack("you have been signed out successfully");

                finish();
            } );
}


public void Pop_Snack(String text){
    Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content),text,Snackbar.LENGTH_SHORT);
    View sbView = snackbar.getView();
    sbView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));

    TextView tv = (TextView) (snackbar.getView()).findViewById(com.google.android.material.R.id.snackbar_text);
    Typeface font = ResourcesCompat.getFont(getBaseContext(), R.font.assistant);
    tv.setTypeface(font);
    tv.setTextSize( 14 );

    snackbar.setAnchorView(findViewById( R.id.bottom_navigation ));

    snackbar.setDuration( 5000 );
    snackbar.show();
}

Thank you

You can create a static boolean variable isloggedOut default to false in the activity where you want to show Snackbar . When you Logged out from the activity set that boolean variable to true and check in the onCreate method of the new Activity where you want to show Snackbar .

if(isloggedOut){
Snackbar snackbar = Snackbar.make(view, "Text to display", 
Snackbar.LENGTH_LONG); 
    snackbar.show();
}

In your signout method

public void signOut(){

auth.signOut();

AuthUI.getInstance()
        .signOut(this)
        .addOnCompleteListener( task -> {
            Intent intent = new Intent(ChangePassActivity.this, SignInActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_CLEAR_TASK |
                    Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            NewActivity.isloggedOut = true;
          

            finish();
        } );

}

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