android - How to Logout from the application

后端 未结 4 1430
情话喂你
情话喂你 2020-12-17 07:12

My Application have 5 activities(A1,A2,A3,A4,A5). Each activity have one text view and one button(B1,B2,B3,B4,B5). If you click on that button then goes to next activity. su

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-17 07:24

    Suppose you have four activities (A,B,C,D) and you want to develop Logout functionality in your app using Menu buttons.

    Step 1: First Declare a Variable in

    SearchHelper.logout=0;//in SearchHelper Class
    //OnCreate of Activity--DashBoard
    if(SearchHelper.logout==1)
    {
        Intent loginscreen=new Intent(this,LoginActivity.class);
        loginscreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Toast.makeText(DashBoardActivity.this, "WELCOME TO LOGINSCREEN", Toast.LENGTH_SHORT).show(); 
        startActivity(loginscreen);
        this.finish();
        SearchHelper.logout=0;
    }
    

    Step 2: onclick on logout button using menu

    Intent homescreen=new Intent(this,DashBoardActivity.class);
    SearchHelper.logout=1;
    homescreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(homescreen);
    this.finish();        
    

    It will redirect to DashBoard Activity then due to SearchHelper.Logout==1 it will again redirect logout Activity. Finally, like that you can logout from anywhere using the menu buttons. without problem of onBackPressed().

    Or use Single instance in manifest For Activity and handle each and every Activity onBackPressed().

提交回复
热议问题