how to close android app when back button pressed - android

I am building a simple login application on android platform.
The 1st Activity is a Login Activity and the 2nd one is a welcome screen.
When a user presses the login button, I start the 2nd Activity by using an Intent.
But in the 2nd Activity, when the user presses the back button, it opens the 1st Activity.
But at this point I want to close my app.
Can I use Fragments?
Any other alternative?

you have to just finish your activity when you switching another activity,
Intent i = new Intent(Login.this, Welcome.class);
startActivity(i);
finish();

you call the finish() in LoginActivity when you start the your SecondActivity
Intent i = new Intent(Login.this, Welcome.class);
startActivity(i);
finish();
OR
you set the android:noHistory = "true" in LoginActivity in AndroidManifest.xml.

you need to create your second activity with FLAG_ACTIVITY_CLEAR_TOP from your first activity.
and then in your second activity you need to call finish() in onbackpressed()
#Override
public void onBackPressed() {
finish();
}

Related

How destroy activity works?

I'm new in android development, and there is something about the life cycle activity that I don't understand, especially with the following example of application that i'm working on.
In my app, I have a Login activity and a Main activity.
In my Login activity, with successful attempt, there is a Intent that start the main activity, and finish() the login activity.
==> There, my login activity is destroyed, so this should not show up again.
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("authentResult", tokenDto);
startActivity(intent);
finish(); //destroy activity to not open it with back button`
In my Main activity, I have a disconnect button that create an Intent that start a (new ?) login activity.
==> Until there, everything's normal, and the login activity is displyed.
Intent loginActivity = new Intent(this, LoginActivity.class);
startActivity(loginActivity);
In the login activity, using the Back button should close the app.
To do that, I send an intent with special flag to the main activity to finish it (So the back button will not wake up the main activity), and then I finish the login activity. The onDestroy method is called and I see the login window close itself.
==> From here I expect the app to be closed. But a "new" login activity shows up, and i suspect that it would be the activity of the first point, so I'm a little lost there...
public void onBackPressed() {
Log.d(TAG, "BACK PRESSED - loginActivity");
//Finish MainActivity
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
finish(); // finish login activity
}
In the onCreate in the mainActivity, I begin with this :
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
Do anyone could explain to me what I'm missing, or show me a better way to close the app directly ?
Don't hesitate to telle me if something's not clear.
If you declare the Login activity as main activity in the Manifest, if you don't destroy it when you launch the second activity then i think the back button will do all you expect without any additional code, and if you press back key on the login activity it will go to phone home screen
On Android applications is the system that decides when to close/kill application.

Clearing android stack before starting a new activity

I have an application with three activities: Login, Register and Main activity.
I want to start the Main activity after a user had logged in or registered.
But, when the user presses the 'back' button, he should not see the activity from which he logged in. In other words, I want to clear the activity stack before starting the Main activity.
I saw a few solutions online, for example: this, this and this.
They did not help.
This is my code for launching the main activity from the Login activity:
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
getApplicationContext().startActivity(intent);
The code in the register activity is quite similar.
I also tried to put finish(); after I started the activity, but it only closed the current activity. Meaning, if I started in the login activity, moved to the register activity and logged in there (Login -> Register -> Main), When I will press the 'back' button I will be return to the Login activity.
Thank you for your help!
Yuval.
The easy way would be to use "noHistory" attribute in manifest. You can set it for the login activity in your manifest file
<activity
android:noHistory="true"
/>
You can do it like this:
If the user goes to the registration screen then just finish the login activity and when they click the back button on their device you can call the onBackPressed () method. In there you can finish the registration screen and start the login activity. If the user went to the registration screen and then registers, you also want to finish the register activity and open the main menu or something else.
Login:
Click on register? Start register activity and finish the login
startActivity (new Intent (Login.this, Register.class));
finish ();
Register:
Click on submit? Start main activity and finish register activity
startActivity (new Intent (Register.this, Main.class));
finish ();
Click om back button?
#Override
public void onBackPressed (){
startActivity (new Intent (Register.this, Login.class));
finish ();
}
And you can also use in the androidmanifest:
android:noHistory="true"
In the activities you want to have no history (where the user can't go to if they click on the back button).
But I prefer to do it in code!
Hope this helps you!
Try this:
Intent intent = new Intent(this, SomeOtherClass.class);
// do not keep this intent in history
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
What you are doing is almost correct, just make a small change in the Intent.
In place of using,
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
getApplicationContext().startActivity(intent);
use:
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
getApplicationContext().startActivity(intent);
It will clear the top, start a new task and clear all the previous task as well. Now, all that Android knows is that there exists a MainActivity in your stack. You can write:
#Override
public void onBackPressed() {
finish();
}
in MainActivity which will allow the user to exit the application and not go back to the LoginActivity.

Finish an activity in android?

I have some sets of activity.
Home Activity -> Activity 1 -> Activity 2 -> Activity 3 -> HomeActivity
finish(); finish(); finish();
Home Activity -> Activity 1 -> Activity 2 -> Activity 3 -> Activity 4 -> HomeActivity
finish(); finish(); finish(); finish();
So now when I am on the final step that is on the HomeActivity if I press back button it again takes me on to the home activity.
How do I finish the home activity without disturbing the whole process? Any help appreciated.
use this flag with your intent in each activity
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
This will not launch home activity again . It will use the previous instance of you Home Activity and bring it to top.
with CLEAR_TOP If the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
You can refer this Link to get more idea about flags.
Whenever you switch from one activity to another by defalut onPause() will be called, if you dont want this activity(in your case home activity), just override onPause() in your home activity and call finish() in onPause().
Allright whenever u call your HomeActivity just add this to your Intent
Intent i=new Intent(yourActivity.this,HomeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
This will clear any previous present activities stack. Hope it helps.
while calling Activity 3 -> HomeActivity
finish()
implement below code :
Intent i = new Intent(Activity3.this,HomeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
call finish on the first home activity as well. i.e when you start activity 'Activity 1' after startactivity() call finish also.
Intent i = new Intent(Activity3.this,HomeActivity.class);
startActivity(i);
HomeActivity.this.finish();

Close all activities of my application

I am starting a new activity with thsese flags
finish_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
when i close the activity it resumes the activity before that how can i close all the activities ?
when you move from the 1st activity to the next activity , the previous activity gets saved on the back stack, this adds the overhead, as objects of same activity are created again when u return back to the previous activity. So you can use the method finish() to kill the 1st activity when moving to the 2nd.
eg:
Intent i = new Intent(this,Myapp.class);
startActivity(i);
finish();
When starting a new activity finish the previous one
Intent i = new Intent(this,CLASSTOLAUNCH.class);
startActivity(i);
finish();

Android close all activity when sing out

In my app have a sign in activity. If login is successful, I start the first activity. It includes the menu, which opens ActivityA, ActivityB, ActivityC... and from these activities I can go deeply ActivityASubA and deeper ActivityASubB...
-ActivityA
--ActivityASubA
--ActivityASubB
-ActivityB
--ActivityBSubA
--ActivityBSubB
...
And if the user open ActivityA and ActivityASubA and later from the menu ActivityB and after that sign out, I open the sign in activity, but in the history will be the previous activities and if he press back see again these activities and it's a problem.
So when sign out I need to close all activity. Which is the best solution?
In sign out method you should put this code:
Intent intent = new Intent(this, Home.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(new Intent(this, Home.class));
finish();
About FLAG_ACTIVITY_CLEAR_TOP you can read th

Categories

Resources