How to close recent activities(press and hold button home) when activity started from services?
Tried android:excludeFromRecents="true" and android:noHistory="true", but it works if I close manual from myActivity. But, when close finish() from services using Broadcast Receiver, it doesn't work.
So, how to close recent activities when it close automatic from services?
Please help . .
This works for clear history from recent apps:
android.os.Process.killProcess(android.os.Process.myPid());
Actually you have to clear the activity stack content. In API level 11 or greater, use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK flag on Intent to clear all the activity stack.
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);
Related
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.
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.
I'm setting up Notifications in my app and I've noticed whilst testing, after clicking a new notifications (in this case the notification loads a blog details page), I have many instances of the blog details activity running (pressing back it shows each activity with the previously loaded blogs).
Is it possible in my Receiver class, so look if there is any instance of ActivityBlog already running, and if there all .finish() them all so there is only ever once instance running?
I found this but I couldn't work out a way to do it from that.
You should study activity launch modes
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
Use android:launchMode="singleTop" in element in manifest file.
You will get callback in onNewIntent() if an instance of activity is already up. Stack of your activities will be automatically updated and there wont be any need of killing activities which is consumes time and resources.
This i believe is the recommended approach.
Intent z = new Intent(Projects_Accel.this,MainActivity.class);
z.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(z);
use this for kill all activity
Do this way
Intent intent = new Intent(this, ActivityBlog.class);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
activity.startActivity(mainIntent);
NOTE:
You need to put android-support.jar in libs folder
My application has the following flow:
Home->screen 1->screen 2->screen 3->screen 4->screen 5>Home->screen 2->Home->Screen 3
My problem is that when I am trying to close the application then Home activity opens everytime when I am trying to close the application.
I just want to close the application when user presses the back key of device on home screen.
There is finishAffinity() method that will finish the current activity and all parent activities, but it works only in Android 4.1 or higher.
This works well for me.
You should using FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK flags.
Intent intent = new Intent(SecondActivity.this, CloseActivity.class);
//Clear all activities and start new task
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
onCreate() method of CloseActivity activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finish(); // Exit
}
Use finishAffinity() method that will finish the current activity and all parent activities. But it works only for API 16+ mean Android 4.1 or higher.
API 16+ use:
finishAffinity();
Below API 16 use:
ActivityCompat.finishAffinity(this); //with v4 support library
To exit whole app:
finishAffinity(); // Close all activites
System.exit(0); // Releasing resources
Sometime finish() not working
I have solved that issue with
finishAffinity()
Do not use
System.exit(0);
It will finish app without annimation.
To clear all the activities while opening new one then do the following:
Intent intent = new Intent(getApplicationContext(), YourActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Add android:noHistory="true" in your activity manifest file.
You can try starting the Screen 3 with Intent.FLAG_ACTIVITY_CLEAR_TASK http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK
Hi if you are in a fragment and are not able to use the finish method as it is(because finish should solve your problem) then you can use the getActivity.finish() method after startActivity(intent);.
If you are not in a fragment you could directly use finish() after you startActivity(intent); line
There are 2 ways for solve your problem
1) call finish() after startActivity(intent) in every activity
2) set android:launchMode="singleInstance" in every tag in menifest file
i think 2nd way is best for solving problem but you can also use first way
I'm trying to resume an activity from within a broadcast receiver's onReceive() method as follows:
Intent i = new Intent(context, TimerSet.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
However the activity (TimerSet.class) is recreated instead of resumed. The only recommended solution I found to this problem was to use the FLAG_ACTIVITY_REORDER_TO_FRONT but I'm already using it.
Also, using Intent.FLAG_ACTIVITY_NEW_TASK doesn't fit my use case but I get the following exception when I do not provide it:
android:util.AndroidRuntimeException: Calling startActivity() from outside of an
Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you
want?
I am not sure whether this is exactly your problem or not, but I have a situation where I got a notification and I want to start my app without starting a new instance (if it's already running)
I finally figured out that these will work. The FLAG_ACTIVITY_NEW_TASK will not start a new instant if the activity has already been running. However, it will add it to the existing stack. Therefore, we can do a FLAG_ACTIVITY_CLEAR_TOP, so back will bring user to the home screen but not the previous state.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
remove FLAG_ACTIVITY_NEW_TASK flag.
also add this flag ->FLAG_ACTIVITY_CLEAR_TOP. This would prevent new activity to be created if already present.