how to perform logout in actionbar

java android sharedpreferences

35 观看

2回复

3 作者的声誉

I need help. I have actionbar in my app. And I want my logout inside of it. How can I do that? I have already these things. But I don't have that session.java. I have attempt to copy others' code but it showed some errors. Please help me. Thanks guys!

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            logout();
            return true;
        }
        return super.onOptionsItemSelected(item);
  }
  private void logout() {
        session.logout();
        startActivity(new Intent(this, MainActivity.class));
        finish();
  }
作者: JOSELITO BROMA 的来源 发布者: 2017 年 12 月 27 日

回应 2


0

68 作者的声誉

you need to use shared preference to keep the session and i think session.logout();this method created in a class where they keep the session using shared preference. ok right now i will tell you how can you keep your login session and how to logout.

step one : when you are trying to login and you got the success response through JSON Api or from your sqlite database just use shared preference and set positive flag value there.

setp two : and then when you click logout button from toolbar in onOptionsItemSelected method just change that shared preference's value to negative.

作者: Imtiaz Dipto 发布者: 2017 年 12 月 27 日

0

3 作者的声誉

Like this sir?



public class LoginActivity extends AppCompatActivity {


        SharedPreferences sharedpreferences;

        public static final String my_shared_preferences = "my_shared_preferences";
        public static final String session_status = "session_status";
        Boolean session = false;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);


            final TextView mShowDialog =(TextView) findViewById(R.id.txtShowDialog);
            final TextView tvRegisterLink = (TextView) findViewById(R.id.tvRegisterLink);

            //Check session login if TRUE then go directly MainActivity
            sharedpreferences = getSharedPreferences(my_shared_preferences, Context.MODE_PRIVATE);
            session = sharedpreferences.getBoolean(session_status, false);


            if (session) {
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                finish();
                startActivity(intent);
            }



            tvRegisterLink.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
                    LoginActivity.this.startActivity(registerIntent);
                }
            });
            mShowDialog.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder mbuilder = new AlertDialog.Builder(LoginActivity.this);
                    View mView = getLayoutInflater().inflate(R.layout.dialog_login,null);
                     final EditText hehe=  mView.findViewById(R.id.etUsername);
                    final EditText hehe1=  mView.findViewById(R.id.etPassword);
                    final Button bLogin =  mView.findViewById(R.id.bSignIn);

                   bLogin.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            final String Username = hehe.getText().toString();
                             final String Password = hehe1.getText().toString();

                            // Response received from the server
                            Response.Listener<String> responseListener = new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    try {
                                        JSONObject jsonResponse = new JSONObject(response);
                                        boolean success = jsonResponse.getBoolean("success");

                                        if (success) {

                                            SharedPreferences.Editor editor = sharedpreferences.edit();
                                            editor.putBoolean(session_status, true);
                                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                            LoginActivity.this.startActivity(intent);
                                        } else {
                                            AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                                            builder.setMessage("Login Failed")
                                                    .setNegativeButton("Retry", null)
                                                    .create()
                                                    .show();
                                        }

                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            };

                            LoginRequest loginRequest = new LoginRequest(Username, Password, responseListener);
                            RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
                            queue.add(loginRequest);
                        }
                    });
                    mbuilder.setView(mView);
                    AlertDialog dialog = mbuilder.create();
                    dialog.show();

                }
            });


        }
    }
作者: JOSELITO BROMA 发布者: 2017 年 12 月 27 日
32x32