简体   繁体   English

startactivity 后显示小吃吧

[英]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".我曾经喜欢一旦用户单击退出,它将移动到登录页面,然后显示snackbar说“您已成功退出”。

How can I make the snackbar appear only after it moved to the new activity?如何让snackbar仅在移动到新活动后才出现? 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 .您可以在要显示Snackbar的活动中创建 static boolean变量isloggedOut默认为false 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 .当您从活动中注销时,将boolean变量设置为true并签入要显示Snackbar的新活动的onCreate方法。

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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