开发者

Logout functionality in android

开发者 https://www.devze.com 2023-04-07 13:17 出处:网络
I am making a kind of social networking app. I am implementing log-out functionality in it. On Logout button click it should navigate to login screen but instead it is now navigating to the home page

I am making a kind of social networking app. I am implementing log-out functionality in it. On Logout button click it should navigate to login screen but instead it is now navigating to the home page screen.I am using the following code for logout..

  IntentFilter intentFilter = new IntentFilter();
  intentFilter.addAction("com.package.ACTION_LOGOUT");
  registerReceiver(new开发者_开发百科 BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {

         System.out.println("onReceive Log out in progress");
          Intent intent1 = new Intent(getApplicationContext(),          Login.class);
                    startActivity(intent1);
                                finish();
                }
            }, intentFilter);


use following for Logout.

yourintent.setflag(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);

It may help you


Simply give the intent to your login activity and put the flag in intent

inten1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

it will clear all the activities and navigate to ur login page.


This is a stack problem. You need to handle it. The best solution I found is that keep single activity in stack when your app runs and on log out only login screen will be in stack and if user presses back button, he will see Home screen.


What worked for me is keeping track of your login state internally, using some sort of global:

public boolean loggedin = false;

and then in all your activities, override onResume() and finish() if you're logged out:

@Override
public void onResume() {
  super.onResume();
  if (!loggedin)
    finish();
}


Try this:

Intent intent = new Intent(getApplicationContext(), Login.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
Toast.makeText(this, "Signed out", Toast.LENGTH_SHORT).show();
startActivity(intent);
finish();


First make these changes to your code

Intent intent = new Intent(getApplicationContext(),Login.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Then remove finish(); written inside your broadcast receiver. Best of luck.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号