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

  • Home
  • SEARCH
  • 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 8050657
In Process

The Archive Base Latest Questions

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

I am working on the android application that required login. I want to check

  • 0

I am working on the android application that required login. I want to check when user press the PostIdea activity button then check the user logged in or not, if not then redirect to the login page else continue with the task.

Here I am using SharedPreferences for that. Now when I press the login button then username & password add to the SharedPreferences and when press the PostIdea Activity then I got the username & password.

BUT my problem is that when I restart the application and go the PostIdea Activity without login then it remember the last login(username/password) of SharedPreferences data.How can I remove that data from SharedPreferences,so that my application works correctly. Let me know where I do mistake or there is any other solution for my problem.

Thanks.

LoginActivity.class

public class LoginActivity extends CheerfoolznativeActivity {

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

    setContentView(R.layout.activity_login);
    setHeader("Login");

    editUser = (EditText) findViewById(R.id.login_useredit);
    editPass = (EditText) findViewById(R.id.login_passedit);
    txterror = (TextView) findViewById(R.id.login_error);
    textlogin = (TextView) findViewById(R.id.login_postidea_textView);
    btngotoregister = (Button) findViewById(R.id.login_btnLinkToRegisterScreen);
    btnlogin = (Button) findViewById(R.id.login_button);
    pgb = (ProgressBar) findViewById(R.id.login_progressBar);

    btnlogin.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            new Login().execute();

        }
    });

}

public class Login extends AsyncTask<Void, Void, Void> {

    int i = 0;
    String uName ;
    String Password;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        uName = editUser.getText().toString().trim();
        Password = editPass.getText().toString().trim();

        if (uName.equals("") | Password.equals("")) {
            Toast.makeText(getApplicationContext(), "Enter the Data",
                    Toast.LENGTH_SHORT).show();

            if (editUser.length() == 0) {
                editUser.setError("Enter Username");
            }
            if (editPass.length() == 0) {
                editPass.setError("Enter Password");
            }
        } else {

            pgb.setVisibility(View.VISIBLE);
        }

    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub


            String loginURL = "http://www.cheerfoolz.com/rest/user/login";

            strResponse = util.makeWebCall(loginURL, uName, Password);

        return null;
    }

    @Override
    public void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

    txterror.setText("");

            try {
                if (strResponse.substring(KEY_SUCCESS) != null) {
                    txterror.setText("");

                    // SharedPreferences Logic
                    SharedPreferences userDetails =getSharedPreferences("userdetails", MODE_PRIVATE);
                    Editor edit = userDetails.edit();
                    edit.clear();
                    edit.putString("username", uName);
                    edit.putString("password", Password);
                    edit.commit();


                    new FetchUserProfileTask().execute();

                } else {
                    txterror.setText("Username and Password Not valid !!!");
                }
            } catch (Exception e) {
                // TODO: handle exception
            }

    }

}

public class FetchUserProfileTask extends AsyncTask<Void, Void, Void> {

    int i = 0;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();


    }

    @Override
    protected Void doInBackground(Void... params) {

        //logic for the featch user profile data

    return null;
    }

    @Override
    public void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

         pgb.setVisibility(View.GONE);
        displayProfile();

    }
}

public void displayProfile() {

    Intent in1 = new Intent(LoginActivity.this, post_myprofile.class);
    in1.putExtra("loginObject", bean);
    startActivity(in1);
}

}

Post_idea_Activity.class

public class Post_idea_Activity extends CheerfoolznativeActivity {

TextView txtwelcome;
String Uname="";
String pass = ""; 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    SharedPreferences userDetails = getSharedPreferences("userdetails", MODE_PRIVATE);
    System.out.println("value of the userdertails ==========>"+ userDetails);
    //String Uname="";
    //String pass= ""; 

    Uname = userDetails.getString("username", "");
    pass = userDetails.getString("password", "");

    Toast.makeText(getApplicationContext(),"username : "+Uname +" \n password :"+pass, Toast.LENGTH_SHORT).show();

    if (Uname.equals(null)) 
    {
        Intent in1 = new Intent(this, LoginActivity.class);
        in1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(in1);

    }
    else {

        setContentView(R.layout.post_idea);
        setHeader("Post idea");

        txtwelcome =(TextView)findViewById(R.id.post_welcome_text);
             }


       }

   //  coding for the layout
}
  • 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-05T07:07:57+00:00Added an answer on June 5, 2026 at 7:07 am

    Clear your shared preferences when your app starts.

    SharedPreferences userDetails =getSharedPreferences("userdetails", MODE_PRIVATE);
    Editor edit = userDetails.edit();
    edit.clear();
    edit.commit();
    

    You can write this in Application class of your application.

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

Sidebar

Related Questions

I am working android application that required login, Now I want After successfully login
I am working on an Android application that requires a user to login before
I am working on an Android application that is required to connect to a
I am working on an android application that determines user location in osm maps.
I'm working on an Android application. I have an activity that has this method:
I am working in an android application and I want to implement a Gallery.
I am working on an android application that requires multiple views which will overlap
I'm new to Android development and working on an Android application that requires the
I'm working on an Android application that has several Activities. In it I have
I'm currently working on an android application that uses a subclass of SimpleListAdapter to

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.