Android kill all other activities on starting a new one - android

I have an android activity problem.
Here is how my process works:
Login Activity starts
Login successful. MainMenuActivity starts and LoginActivity is finished by me.
User touched on settings and SettingsActivity starts. MainMenuActivity is NOT finished. because is it the main menu. when user presses the back on settings screen I need to go back MainMenuActivity. so I cant kill MainMenu.
User touched on log out and SettingsActivity is finished by me and Login activity starts. As user returns the login I need to kill MainMenuActivity but I cant.:/
I tried FLAG_ACTIVITY_SINGLE_TOP, CLEAR_TOP, SINGLE_TASK, NEW_TASK, NO_HISTORY etc.. almost all of them didnt work
I put launchMode="singleTask", clearTaskOnLaunh="true" etc. didtn work again.
I tried addFlags() and setFlags() both, didnt work
There are some many issues about this topic here, I read and applied all the suggested solutions and didnt work.
Can anyone help, please?
P.S android:minSdkVersion="8" and android:targetSdkVersion="15" for my app. I didnt use fragments in the app, I use old activity structure.

Use the combination of two flags like this:
Intent intent = new Intent(this, Login.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
This deletes all the other activities and starts this one.
Try this.
For api level <11
i.addFlag(Intent.FLAG_ACTIVITY_NO_HISTORY |
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

I'd suggest to start the settings activity for result (see here), and when the user requests to logout, set a result accordingly. You will get this result in MainActivity's onActivityResult (see here) and can handle the logout there, finishing the mainActivity before starting the loginActivity.

I suggest that you don't finish the Login activity when you start the main menu. Then you can always clear all activities on logout by doing this:
Intent intent = new Intent(this, Login.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will only work if Login activity is still active at the root (beginning) of the task stack.
To prevent the user from BACKing into the Login activity from the Main activity, you can override onBackPressed() in Main activity and do something else.

Use FLAG_ACTIVITY_CLEAR_TOP like this-
Intent loginIntent = new Intent(this, Login.class);
loginIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
finish();

Intent intent = new Intent(MainActivity.this, MyActivity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
in this example Mainactivity automatically closed and Myactivity2 Start.
and one thing you need to add finish();

Related

Best way to handle activity state

I'm making an app that shows another activity when I correctly login however in the home screen when I pressed back and then returned to the app the login activity showed.
I was able to avoid this overriding the onBackPressed this way:
public void onBackPressed()
moveTaskToBack(true);
}
Is this the best way to do it? Is there a more proper way to keep the state of the application when I exit it?
In your login activity after starting intent call finish()
finish destroy your activity and avoid to run it again automatically.
Your issue is that you kept LoginActivity in stack. so when you press back it will kill MainActivity(after login) and since LoginAcitivity still there. it will be shown again. best is to kill LoginAcitivty after starting new activity.
call this in LoginActivity:
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
finish();
As you question I understand, you want to clear the entire history stack and start a new activity. For this, in API level 11 a new Intent Flag was added which is: Intent.FLAG_ACTIVITY_CLEAR_TASK
Just to clarify, use this:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
For API <=10 now one can use IntentCompat class for the same. One can use IntentCompat.FLAG_ACTIVITY_CLEAR_TASK flag to clear task. So you can support pre API level 11 as well.

Previous activity stays on activity stack

I'm using this library which is a Wizard.
When user is done with the Wizard and clicks Done button he moves to another Activity.
I have added to Manifest file android:noHistory="true" for the WizardActivity and also when I start the next activity im doing it like this :
Intent intent = new Intent(this, ProjectsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Nevertheless when user click backs he still goes to the WizardActivity. I'd like the user either to exit or go the activity that started the WizardActivity.
Thank you
finish() the activity after launching the other Activity. i.e after startActivity(intent) add 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.

How to Clear the activity stack SDK < 11

I need to clear the activity stack. All the activity except the current one in SDK 8. I want after logout from any activity I can reach to the Login Activity and All previous Activity will get Removed from the activity stack.
I have tried this solution
Intent i = new Intent(BaseActivity.this, LoginActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
But it is not working. I can't use finish() because user can logout from any activity.
Your Help is Highly appreciable.
Just use this flag on its own Intent. FLAG_ACTIVITY_CLEAR_TASK
Use This
Normally when we launch new activity, it’s previous activities will be kept in a queue like a stack of activities. So if you want to kill all the previous activities, just follow these methods
Intent i = new Intent(OldActivity.this, NewActivity.class);
// set the new task and clear flags
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(i);
I have got two Beautiful solution of my problem.
SOLUTION 1:
One is by using Broadcast Receiver. That I can broadcast a message to all my activity and onRecive() I can finish all my activity. As this Link says.
SOLUTION 2 (A Much Easier way):
Second is by managing a flag in ShearedPrefrences or in Application Activity. In starting of app (on splash screen) set the flag = false; OnLogout click event, just set the flag true and in OnResume() of every activity check if flag is true then call finish().
It works like a charm :) And please If you can't answer at least don't abuse the question.

Closing an android application programatically in android

I have a application that has two activities
Activity1
Activity2
Now i start Activity1 and move to Activity2 now i want to close the application at a moment
If i use finish() i am able to close only Activity2 and not
Activity1
What i want to do ::
i want my application to quit(close all activities and go to
homescreen)
But i don't want my apk to be removed from android system itself
How can i achieve this ?
{Edit}
I used the code(Activity2 has a fragment which launched a dialog on
"ok" condition in positive condition i am performing this condition below)
I am able to launch the previous activity but it is not canceled, its
just reloaded
Intent intent = new Intent(getActivity(), SplashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
getActivity().finish();
You need to make your Activity2 as "top task" with a Flag when you call it as follows:
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
See the Reference:
If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished.
Then, call finish() in your Activity2. This should do the trick.
There is a way to kill your application process, but it is not recommended
this.finish();
Process.killProcess(Process.myPid());
This will kill your application and releases all the memory associated with it.
Note: Assuming your app has a single process id
Do like this
Intent intent = new Intent(Activity1.this, Activity2.class);
startActivity(intent);
Activity1.this.finish();
Intent intObj=new Intent(this, Homescree.class);
intObj.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intObj);

Categories

Resources