How to close activity, so cannot be opened again from active apps? - android

i have custom activity which is called from background by receiver.
After on button click i want to close activity, so cannot be opened again if user holds menu button and list of active apps is displayed.
How can i do it? I tried use finish() method and kill process, but without luck..i can always display activity from list of apps again.
Thanks for any help.
Edit:
I Just added
<activity android:noHistory="true"
But after finish the activity i am able to get back into the activity (See image below).
Activity is started from receiver with following flags (maybe problem is here?)
intentOne.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

Try add android:launchMode="singleInstance" android:excludeFromRecents="true" android:noHistory="true" and in AndroidMainfest

just add android:noHistory="true" to the activity manifest.
http://developer.android.com/guide/topics/manifest/activity-element.html#nohist
android:noHistory
Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen — "true" if it should be finished, and "false" if not. The default value is "false".
A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.
This attribute was introduced in API Level 3.

Related

How to prevent stacked activity from showing when app is re-launched?

I have an annoying issue with the activity stack that I haven't found a solution for.
Basically I have an activity that acts as a "starter" activity (the main activity in my manifest, this is started from the launcher etc). It is translucent, set using:
android:theme="#android:style/Theme.Translucent.NoTitleBar"
What it does is check the Intent that is fed to it. If the intent data is empty, it starts a new activity which is the main activity for the app.
If the intent data contains certain commands the starter activity should perform certain tasks and then exit, not even starting the main activity. So this should happen without any UI (except for a popup message when done).
My problem is that if the main activity has been started, if the user uses the home button to leave it, the next time the starter activity is started with a command, the main activity also shows up briefly.
I'm assuming this is because of the activity stack since I'm not restarting the main activity from the starter activity in this case.
I've tried various solutions to no avail. I can't use finish() in the main activity in onPause or onStop since that also exits the activity if the user for example enters the settings activity and that is not wanted behavior. I also tried variations of re-launching the starter activity with
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
but that doesn't seem to work either.
The thing that is weird is that when this unwanted behavior happens, onCreate/onResume/onStart is not called on the main activity. Still it shows! I'm guessing this is because it is stacked and since the startup activity is translucent, the main activity is shown through it.
Enable the android:noHistory attribute on your activity within your manifest:
<activity
...
android:noHistory="true">
...
</activity>
This will set the activity to be removed from the activity stack when it starts the next activity. The user will not be able to return to an activity that has android:noHistory="true".
See:
Activity Attribute: android:noHistory

Finish leaves activity in stack

I have an activity that performs a small bit of code and then calls finish. This works fine and the activity goes away, but if I press the history button (if it's called the history button?) the activity still appears to be there, though clicking it will do nothing.
I have enabled android:noHistory="true" in my manifest. I only have a single activity that changes some settings, and then calls finish, nothing else.
Is there a way for me to make that trace of the activity go away?
Assuming that the history button you refer to is the list of recent apps, you can set
android:excludeFromRecents="true"
By default value for this attribute is false.
For other attributes check the link: http://developer.android.com/guide/topics/manifest/activity-element.html

Current Activity disappearing when screen gets locked

So here is my activity.
<activity
android:name=".MainActivity_Hard"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/title_activity_main_activity__hard"
android:noHistory="true"
android:screenOrientation="portrait" >
</activity>
I marked it as android:noHistory="true" because I did not want the screen to be added to the activity stack.
But however when I lock the screen and unlock it. The activity disappears and the previous screen appears. This should not happen. Could anyone tell me why.
Note: It happens in ICS (Samsung Galaxy Note), but not with previous devices (Gingerbread,etc.)
Setting the noHistory attribute in your activity means that as soon as the user navigates away from that activity--bringing up the lock screen included--its finish() function is called automatically, thus destroying it and going back to the previous activity that spawned it.
This is typically used for splash-screen-type activities which, for example, acts only as a launcher and you don't want the user to be able to go back to the splash screen from the child.
If this is not your intention, then you should consider removing the noHistory attribute and maybe managing the activity lifecycle yourself. (For example, if you don't want your activity to be in the stack, then just call finish() just after it starts another activity.)
android:noHistory
Whether or not the activity should be removed from
the activity stack and finished (its finish() method called) when the
user navigates away from it and it's no longer visible on screen —
"true" if it should be finished, and "false" if not. The default value
is "false". A value of "true" means that the activity will not leave a
historical trace. It will not remain in the activity stack for the
task, so the user will not be able to return to it.
I don't know why. But if you don't want to add this activity to current stack. You can set android:launchMode="singleTask". When start this activity it will be added to it own stack.
You can see more here enter link description here
Hope it help :)
In AndroidManifest.xml put this permission tag above application tag.
<uses-permission android:name="android.permission.WAKE_LOCK" />

androidManifest activity android:excludeFromRecents

I am using following code to open an activity as a popup window using
<activity android:name=".RadioButtonExample"
android:theme="#android:style/Theme.Dialog"
android:excludeFromRecents="true"/>
But excludeFromRecents is not working. If user clicks back button the popup window shows while retrieving back to main menu.
Use android:noHistory="true"
Whether or not the activity should be removed from the activity stack
and finished (its finish() method called) when the user navigates away
from it and it's no longer visible on screen — "true" if it should be
finished, and "false" if not. The default value is "false". A value of
"true" means that the activity will not leave a historical trace. It
will not remain in the activity stack for the task, so the user will
not be able to return to it.

How can I disable 'go back' to some activity?

I don't want the user to be able to go back to the splashscreen of my app. One solution seems to be to check if the activity below the current one is an instance of the splashscreen, and in that case exit the app, as shown in the code below. However, I don't know how to check what's the previous activity in the stack. Anybody can help? Is there any other way to disable 'go back' to a given activity?
#Override
public void onBackPressed() {
if(<previous activity in stack is an instance of splashscreen>){
Intent exit_intent=new Intent(CurrentActivity.this, SplashScreen.class);
exit_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
exit_intent.putExtra("EXIT", true);
context.startActivity(exit_intent);
}
}
Call finish() in your Splash Screen activity right after starting the next activity.
Another approach is to add this attribute to your activity in AndroidManifest.xml: android:noHistory="true"
Example:
<activity android:name=".SplashActivity" android:noHistory="true"/>
This attribute instructs Android to remove SplashActivity from the history stack once its navigated away from.
Just call context.finish() after context.startActivity()
try the following when calling the next Activity from your Splashscreen:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
<activity android:name=".SplashActivity" android:noHistory="true"/>
From the documentation:
Whether or not the activity should be removed from the activity stack
and finished (its finish() method called) when the user navigates away
from it and it's no longer visible on screen — "true" if it should be
finished, and "false" if not. The default value is "false".
A value of "true" means that the activity will not leave a historical
trace. It will not remain in the activity stack for the task, so the
user will not be able to return to it. In this case,
onActivityResult() is never called if you start another activity for a
result from this activity.
This attribute was introduced in API Level 3.

Categories

Resources