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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:05:49+00:00 2026-05-22T19:05:49+00:00

I was able to open the app and it starts by opening the login

  • 0

I was able to open the app and it starts by opening the login screen and after login it takes you to the MainActivity. From MainActivity there is no back to login and it would stay in session until logout. But when I exit the app it would go back to the login screen even tho I have a Sharedpref which saves the username in session. I added nohistory=true in the manifest. In MainActivity I put Intent mainactivity.class(if press back button) this reason was because when I:

logout > login > mainactivity 

And press back button it would go back to logout screen (login should be okay because of myhistory=true). My question is how can I keep username in session even if the app is exit out.

Links below doesn’t help me much

What is the correct way of creating a login screen/activity in Android?

Prevent showing Login screen after user login

How to presist Login credentials and do auto-login in Android

My code

LOGIN.java

    public class Login extends Activity {
    private EditText etUsername;
    private Button btnLogin;
    private Button btnCancel;
    private TextView lblResult;

    @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event)  
    {          
        if(keyCode==KeyEvent.KEYCODE_BACK)  
        {             
            this.startActivity(new Intent(Login.this,Login.class));  
        }  
        return true;  
    }  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

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

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

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

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

Logout.java

    public class LogoutActivity extends Activity {
    private Button btnLogout;
    private Button btnCancel;
    private TextView lblResult;
    private EditText code;
    @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event)  
    {  
       if(keyCode==KeyEvent.KEYCODE_BACK)  
        {  
            this.startActivity(new Intent(LogoutActivity.this,MainActivity.class));  
        }  
        return true;  
    }  

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

                SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                SharedPreferences.Editor editor = settings.edit();
                editor.remove("username");

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

                    Intent i = new Intent(getApplicationContext(), Login.class);
                    startActivity(i);
                } else {
                    lblResult.setText("Logout failed");

                }

            }
        });

         btnCancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

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

            }

    });
  • 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-22T19:05:50+00:00Added an answer on May 22, 2026 at 7:05 pm

    In Login.onCreate(), check for the user ID in the SharedPref. If it exists, go directly to the MainActivity.

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

Sidebar

Related Questions

I want to be able to restart the app when coming back from background.
I'm building an Open Graph App that should be able to publish, retrieve, and
I want to stop outgoing call and open the application. The app is able
I want: To be able to open a form, select an item from a
I would like the designer to be able to open the same solution file
I'm working on an app that needs to be able to open a PDF
I'd like to be able to open the App GUI and have it automatically
I am not able to open an excel file to write data using following
I am not able to open a url for read() using urllib or urllib2
I'm not able to open an infoWindow when I select a polygon. Here's my

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.