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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:20:45+00:00 2026-06-07T02:20:45+00:00

When i press back button on my home screen to exit from my app

  • 0

When i press back button on my home screen to exit from my app its not happening. It is going in a loop with my login window so how can i exit from my app??? m new to android
here is my home screen code

public class Home extends Activity implements OnClickListener {
    Button btnLinkToRegister;
    Button btnLinkTologin;
    Button btnLinkToCompanyRegister;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);

        btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegister);
        btnLinkToCompanyRegister=(Button)findViewById(R.id.btnLinkToCompanyRegister);
        btnLinkTologin=(Button) findViewById(R.id.btnLinkTologin);
        btnLinkToRegister.setOnClickListener(this);
        btnLinkTologin.setOnClickListener(this);
        btnLinkToCompanyRegister.setOnClickListener(this);
    }           

public void onClick(View v) 
{
switch(v.getId())
{

case R.id.btnLinkToRegister:
 Intent i = new Intent(getBaseContext(), Registration.class);
 startActivity(i);
 break;

case R.id.btnLinkTologin:
    Intent j = new Intent(getBaseContext(), Login.class);
    startActivity(j);
    break;

case R.id.btnLinkToCompanyRegister:
    Intent k = new Intent(getApplicationContext(), CompanyRegister.class);
    startActivity(k);
}

}


}

here is login activity

public class Login extends Activity implements OnClickListener 
{

 Button mLogin;
 Button mRegister;

 EditText muname;
 EditText mpassword;

 DBHelper DB = null;
public String status="";



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mRegister = (Button)findViewById(R.id.register);
        mRegister.setOnClickListener(this);

        mLogin = (Button)findViewById(R.id.login);
        mLogin.setOnClickListener(this); 


    }


 public void onClick(View v) 
 {
 switch(v.getId())
 {

 case R.id.register:
  Intent i = new Intent(getBaseContext(), Registration.class);
  startActivity(i);
  break;

 case R.id.login:

  muname = (EditText)findViewById(R.id.Ledituname);
  mpassword = (EditText)findViewById(R.id.Leditpw);

  String username = muname.getText().toString();
  String password = mpassword.getText().toString();



  if(username.equals("") || username == null)
  {
   Toast.makeText(getApplicationContext(), "Please enter User Name", Toast.LENGTH_SHORT).show();
  }
  else if(password.equals("") || password == null)
  {
   Toast.makeText(getApplicationContext(), "Please enter your Password", Toast.LENGTH_SHORT).show();
  }
  else
  {
   boolean validLogin = validateLogin(username, password, getApplicationContext());

   if(validLogin)
   {        
       if(status.equals("s"))
       {
    Intent in = new Intent(getBaseContext(), JSHomePage.class);
    in.putExtra("UserName", muname.getText().toString());
    startActivity(in);
       }
       else if(status.equals("c"))
       {  Intent in = new Intent(getBaseContext(), CompView.class);
        in.putExtra("UserName", muname.getText().toString());
        startActivity(in);}
   }
  }
  break;

 }

 }


 private boolean validateLogin(String username, String password, Context baseContext) 
 {
  DB = new DBHelper(getBaseContext());
  SQLiteDatabase db = DB.getReadableDatabase();

  String[] columns = {"_id","status"};

  String selection = "username=? AND password=?";
  String[] selectionArgs = {username,password};

  Cursor cursor = null;
  try{

  cursor = db.query(DBHelper.Login_Table, columns, selection, selectionArgs, null, null, null);

  startManagingCursor(cursor);

  }
  catch(Exception e)

  {
   e.printStackTrace();
  }
int numberOfRows = cursor.getCount();

  if(numberOfRows <= 0)
  {

   Toast.makeText(getApplicationContext(), "User Name and Password miss match..\nPlease Try Again", Toast.LENGTH_LONG).show();
   Intent intent = new Intent(getBaseContext(), Login.class);
   startActivity(intent);
   return false;
  }

  cursor.moveToNext();
  status = cursor.getString(cursor.getColumnIndex("status"));
  startManagingCursor(cursor);
  return true;

 }
 public void onBackPressed() 
    {
        Intent i = new Intent(Login.this, Home.class);
        startActivity(i);
    }

 public void onDestroy()
 {
  super.onDestroy();
  DB.close();
 }
}
  • 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-07T02:20:47+00:00Added an answer on June 7, 2026 at 2:20 am

    I guess you try to achieve something that look like that :

    HomeActivity -> buttonClicked -> LoginActivity -> BackPressed -> HomeActivity -> BackPressed -> Exit your app

    In your case, removing the onBackPressed overrided method should do the trick ( Android manage navigation between activities with the back key automaticaly ).
    Your loop probably come from the call to startActivity wich add a new activity to the stack rather than destacking. And you probably need to call for super if you override onBackPressed(), not sure thought.

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

Sidebar

Related Questions

I have an app that crashes if i press the home button, back button
I press login button to login into my app and I'm using asyncTask for
I need to know if we press home or back button in android... can
I'm having a requirement where i can press a logout button from any activity
I need to show alert with yes no button on back key press for
Steps: I start my Android app. I press the Menu button and select Preferences
How can I use the Android back button to move back within my app
I have an incredibly frustrating crash that only occurs after I press home button
When you press the home button, what does it do by default? I want
Just need a little bit of Javascript to warn people that press the back

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.