I have two activities (A and B) in my app and some BroadcastReceiver.
I encounter the following scenario:
A is running and was closed using the home button (onStop was called).
Some time after, BroadcastReceiver was triggered with some intent. It run the following code:
Intent activityIntent = new Intent(context,
B.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(activityIntent);
And B is indeed started, however A is also brought to front (behind B). How could I avoid A being fronted?
when you are pressing home button you are not closing the app actually(i mean it's in pause state) and it's in back stack and whenever you started a new activity of same app and close that activity it pops out the top activity in back stack.. So if you don't need this thing happen then please try following code
#Override
public void onStop() {
if(!isFinishing())
finish();
super.onStop();
}
From BoardcastReceiver you can start Activity B using this intent filter. This will clear the activity stack and pop Activity A from the stack.
Intent intent = new Intent(context,B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
Related
I have 2 activities (MainActivity,SecondActivity)
I am starting SecondActivity from MainActivity now the situation is when I click onbackpress it's return to MainActivity without finishing the second activity
How I can go back to the second activity without reinitializing the activity again?
I created foreground service to display a notification and added the flag start UpdateCurrent to the pending intent but this cause that Oncreate recalled
what is the solution for this ?
If my understanding of your issue is correct, you need to try this:
Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent.FLAG_ACTIVITY_SINGLE_TOP
And this:
Intent int = new Intent(SecondActivity.this, MainActivity.class);
int.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(int);
I want to finish my activity, tried:
finish();
Activity.this.finish();
finishAffinity();
killing process...
It's how I am opening activity from service:
Intent a = new Intent(getApplicationContext(), Axctivity.class);
a.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);
stopSelf();
After finish, activity is succesfully hidden but still stay on runnining apps list when I can restore it.
It's how finish() works. It doesn't destroy the object nor the activity.
The android OS keeps the processes around to speed up getting back into the applications.
See what exactly Activity.finish() method is doing?
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.
I have A and B activities. A is singleTop on launch, B is excludeFromRecents. When I back from B to A, B is always destroying. How to prevent it? When I press Home then B is just stopped and not destoying. Thanks.
Following back code if B activity:
Intent intent = new Intent(this, ActivityPlaylist.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Without flags back not working at all.
I also have prevention of creating new instance in onCreate of A reason of I launch app from Eclipse (bug):
if (!isTaskRoot()) {
// Android launched another instance of the root activity into an existing task
// so just quietly finish and go away, dropping the user back into the activity
// at the top of the stack (ie: the last state of this task)
finish();
return;
}
Upd: my case is I need do not lose B condition, including list position and so on.
I'm using this way to start activity from Service, if my app is background:
Intent intent = new Intent(this, FromBackgroundActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
it works perfect,
but if FromBackgroundActivity is active and I press Home key it puts all to background.
And if I click on my app in taskbar it restores my app to foreground but without FromBackgroundActivity, it is destroyed. (
how can i restore my app from background with FromBackgroundActivity?
Try intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
It'll clear all the task and go to the FromBackgroundActivity.