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 6002429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:58:37+00:00 2026-05-23T00:58:37+00:00

For a few days, I have been trying to get the username to save

  • 0

For a few days, I have been trying to get the username to save in session properly. First, I opened the app and it would open login screen. I login with username, then it would take me to the MainActivity. Now I exit the app and reopen the app to ensure that username is saved and it would pass the login screen and go straight to the MainActivity. So far so good, then I would logout and it would go back to the login screen. I want to make sure that it is completely logged out so i exit the app and reopen the app and it would take me to the MainActivity. I couldn’t figure out how to fix that problem.

my login class

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

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("username", username);
        editor.commit();


        if(prefs.getString("username", null)!=username)
        {Intent i = new Intent(getApplicationContext(), Customer.class); 
        startActivity(i);}


        etUsername = (EditText)findViewById(R.id.username);
        btnLogin = (Button)findViewById(R.id.login_button);
        btnCancel = (Button)findViewById(R.id.cancel_button);
        lblResult = (TextView)findViewById(R.id.result);

        btnLogin.setOnClickListener(new OnClickListener() {
            //@Override
            public void onClick(View v) {
            // Check Login
            String username = etUsername.getText().toString();

            if(username.equals("1111")){
                lblResult.setText("Login successful.");

                 Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);

my logout class

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

        code = (EditText)findViewById(R.id.codeout);
        btnLogout = (Button) findViewById(R.id.submit);
        btnCancel = (Button) findViewById(R.id.cancel);
        lblResult = (TextView)findViewById(R.id.result);

        btnLogout.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String logout = code.getText().toString();

                if (logout.equals("99")){
                    lblResult.setText("Logout successful");

                    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

                    SharedPreferences.Editor editor = prefs.edit();
                    editor.remove("username");
                    editor.commit();

                    Intent i = new Intent(getApplicationContext(), Login.class);
                    startActivity(i);

                    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-05-23T00:58:37+00:00Added an answer on May 23, 2026 at 12:58 am

    Unless I am missing something, it looks like you are setting username in your preferences and then immediately checking that value. It appears that this if statement would always pass:

    if(prefs.getString("username", null)!=username)
    

    Also, as @inazaruk pointed out, you should use equals() instead of == for a String comparison.

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

Sidebar

Related Questions

I have been racking my brain for a few days trying to get a
For a few days now, I have been trying to get my Java project
I've been trying for a few days now to get Google App Engine to
I have been trying for a few days now to get the composite key
For a few days now I have been trying to get an ASMX webservice
I have been trying to get this to work for a few days. Basically
I have been trying for a few days to get Sonata Media Bundle working
For the past few days I have been trying to play any sound over
I have been struggling for a few days trying to find out how come
I have been trying to figure this out for a few days now and

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.