Logout functionality in android

﹥>﹥吖頭↗ 提交于 2019-12-21 20:19:24

问题


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

回答1:


use following for Logout.

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

It may help you




回答2:


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.




回答3:


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.




回答4:


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



回答5:


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



回答6:


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.



来源:https://stackoverflow.com/questions/7524904/logout-functionality-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!