Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide login activity

Tags:

android

I have LoginActivity which checks SharedPreferences for login details then it redirects to HomeActivity and other activities after that. I have put Menu item Sign Out on each of this activities and used this code on sign out button.

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

How to hide the Login activity, so that when user press back button from home screen it'll close the app.

Like when I open app it shows Home screen and when I press back button normally it should close the app. But in my case it takes me to the Login screen which is the first screen checking user credentials.

I cannot end the Login activity, otherwise that solution doesn't work.

I'm a new to Android. Please suggest something to solve this problem.

like image 283
SkyWalker Avatar asked Feb 06 '12 05:02

SkyWalker


2 Answers

you should try this on your home Activity's back key function:

onBackpress(){
 Intent intent = new Intent(mContext, LoginActivity.class);
 intent.putExtra("FLAG", 0);
 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
 startActivity(intent);
}

and on your LoginActivity just do:

 onNewIntent(Intent intent){
   int i = intent.getIntExtra("FLAG", 0);

   if(i == 0) 
      finish();

 }

remember launchMode for activity in menifest should be singleTop.

like image 122
Anand Tiwari Avatar answered Oct 20 '22 11:10

Anand Tiwari


Override the Activity.onBackPressed() method and then send the application home via an Intent.

From the SDK:

An intent with the following categories will allow you to go home.

ACTION_MAIN with category CATEGORY_HOME -- Launch the home screen.

like image 24
JoxTraex Avatar answered Oct 20 '22 10:10

JoxTraex