Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8070529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:25:07+00:00 2026-06-05T13:25:07+00:00

I am developing an app in which I need to manage users session i.e

  • 0

I am developing an app in which I need to manage users session i.e when user logs in for the first time he must see the login page and once he is authenticated he is redirected to the home screen and tht time a value is set in sharedpreferences. Now on the home screen when the user clicks on logout button the values in shsredpreferences must be cleared and the next time the user opens the app he must be directed to login page.Unless and until user clicks logout he must not be shown the login page.
I am able to store values in sharedpreferences but not able to delete them.

here is my code for loginpage.java

package com.sess.eg;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class loginpage extends Activity {
    /** Called when the activity is first created. */


        EditText acc,user,pin;
        Button login;
        StringBuilder builder = new StringBuilder();
        String UserName;

        SharedPreferences.Editor prefs;
        @Override

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);


//            SharedPreferences prefs1  = getPreferences(MODE_WORLD_READABLE);
//          UserName= prefs1.getString("User", "Abcdef");
//          
//          System.out.println(UserName);
//          
//          if(UserName.equals("Ad"))
//              
//            {
//              System.out.println(UserName);
//              Intent i=new Intent(loginpage.this,homepage.class);
//              startActivity(i);
//            }

            setContentView(R.layout.main);
            acc = (EditText) findViewById(R.id.ed_login_acc);
            user = (EditText) findViewById(R.id.ed_user_acc);
            pin = (EditText) findViewById(R.id.ed_pin_acc);
            login = (Button) findViewById(R.id.login_button);

            SharedPreferences prefs1  = getPreferences(MODE_WORLD_READABLE);

            login.setOnClickListener(new Button.OnClickListener(){

                public void onClick(View v) {




                    prefs.putString("User", "Ad");
                    prefs.commit();
                    //System.out.println(sendJson1());
                    //startService(new Intent(Login.this, MyService.class));
                    Intent i = new Intent(loginpage.this, homepage.class);
                    startActivity(i);
                    finish();

                    //System.out.println(UserName); 
                }

            });



        }
         @Override
        protected void onStart() {
            SharedPreferences prefs1  = getPreferences(MODE_WORLD_READABLE);
            UserName= prefs1.getString("User", "Abcdef");

            System.out.println(UserName);

            if(UserName.equals("Ad"))

             {
                System.out.println(UserName);
                Intent i=new Intent(loginpage.this,homepage.class);
                startActivity(i);
                finish();

             }
            super.onStart();
        }
    }


here is my code for homepage.java
package com.sess.eg;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class homepage extends Activity{

    Button logout;

     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.home3);

            logout = (Button) findViewById(R.id.logout);

            logout.setOnClickListener(new Button.OnClickListener(){

            public void onClick(View v) {

            SharedPreferences.Editor prefs = getPreferences(MODE_WORLD_WRITEABLE).edit();


            prefs.clear();

//          prefs.commit();
            SharedPreferences prefs1 = getPreferences(MODE_WORLD_READABLE);
            String UserName= prefs1.getString("User", "Abcdef");
            System.out.println(UserName);
            finish();

                }
            });

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:25:08+00:00Added an answer on June 5, 2026 at 1:25 pm

    In your code:

    //prefs.commit();
    

    Change this. Remove it from the comments.

    prefs.commit();
    

    commit() is called to save your preferences changes.

    “Commit your preferences changes back from this Editor to the SharedPreferences object it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.
    Note that when two editors are modifying preferences at the same time, the last one to call commit wins.
    “

    Read here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing an app in which i need to use map kit(first time)
Im developing a app which has a user sign-up activity, there I need to
I am developing an android app in which i need to display images after
I'am developing an app which requires drawing the user signature. On click of a
I'm developing an app which will have a central database users can add entries
I am developing an app which uses gps. i need a high accuracy so
I am developing a app in android .... which need a code snippet to
I am developing an application which is based on user challenges. The app also
I am developing an app which allows the user to customise every component such
I am developing a Catalyst app which uses Template::Toolkit as template engine. One page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.