How destroy activity works? - android

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.

Related

how to close android app when back button pressed

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();
}

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.

Starting Main launcher activity from another activity

my Main launcher activity (the one that has android.intent.action.MAIN as its action) is Login page.
After successful login I started HOME activity and finish() the LOGIN one to prevent users returning to that page by pressing BACK button.
When I press SIGN OUT button, I want the app to return to Login page. But I can't find a way to do it. Here's the sign out code:
//This method is in HOME activity
private void signOut(){
Intent i = new Intent("android.intent.action.MAIN");
startActivity(i);
finish();
}
That code will open dialog box listing all applications in my phone for me to choose. I tried putting the package name + class name (com.example.test.Login) as the Intent but keep getting this error:
android.content.ActivityNotFoundException: No Activity found to handle Intent
I know I can trick this problem by using Splash screen as Main activity. But If there is another better solution, I want to know it.
Thanks
Use
Intent i = new Intent(this, LoginPage.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

Hide login activity

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.
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.
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.
You can override the back button keypress event and have it close the app when pressed.

Categories

Resources