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

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

Related

Activity not destroying properely and multiple instances are created which are not required

I have an application with two activities A and B. On clicking on list item in activity A it takes me to the activity B.
For the first time the activity works fine.
Now I have a broadcast reciever which throws me a notification , on clicking on that notification it takes me to the activity B.
The problem is when i am allready on activity B and the new notification comes and when i click on it it relaunches the same activity.When i press back the activity is not destroyed properly as i am getting logs of previous Activity B when i am currently on Activity B.
I have tried out allmost all suitable flags like Intent.Flag_Clear top etc...
But it isnt helping me out.
My activity sometimes works fine and sometimes doesnt work properely.
Please help me out stuck on this problem since a week
I think the launchmode attribute in your Manifest will do the trick. See http://developer.android.com/guide/topics/manifest/activity-element.html and scroll down to the launchmode.
singleTask or singleInstance sounds like it is what you want
You can change the launch mode of your activity on the manifest file. See this activity attribute: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
in AndroidManifest.xml use android:noHistory="true" in your activity. This will destroy your activity B destroyed whenever you leave it.

How to start android activity without showing main activity in the back

I'm developing an application containing 2 activities:
Main activity launched where user starts the app.
Second activity is launched where user clicks on a button widget
on the home screen.
I created my widget and the view that will be displayed when the user clicks on the widget button.
My problem: The second activity UI is displayed above the main activity, the main activity is visible because the second activity layout is transparent, because I want to let the home screen still be visible when clicking in the widget.
How can I launch only the second activity without launching the main activity below it? I tried to remove <action android:name="android.intent.action.MAIN" /> in the main activity from the manifest file, widget works but when I install my app in the phone there's no app icon installed so I can't launch the main activity.
I hope that I have expressed my problem.
You have to finish the first Activity right after you start the second Activity
startActivity(<secondActivity>);
finish();
You can either finish your main activity after you start your second activity:
startActivity(secondActivity.class);
finish();
Or in your application manifest you can use the noHistory flag on your main activity, so it calls finish itself when you navigate away from it.
android:noHistory="true"
Perhaps having a different intent action and intent filter associated with the second activity will work. Have a look at the documentation. The notepad application example is should be similar to what want need to do.
I solved my problem like this :
1- I added to the main activity attributes in the manifest file :
android:launchMode="singleTask"
I tested it does not makes loading time when re-launching activity longer than usually because this attribute does not erase the app cache and keeps the intent instance in the memory.
2- Add this to main activity class :
#Override
void onPause()
{
super.onPause();
finish();
}
Thank you all for your help.

Android Startup process and Main Activity - Design Pattern

I have the following problem, and I would like to design the implementation to make sure I will not encounter any issues.
At application startup, I need to do one (and only one) server request to keep the phone up to date. Then I enter into the main activity.
My biggest issue is the Back button with Android, which can potentially bring back my stack to the very first activity, ie, the one that does server synchronisation.
I have thought of the following implementation:
I launched the Main activity straight away
In the onCreate() of the Main Activity, I launch the synchronisation process ... with some background logos, progress bar, etc etc...
Upon completion of the synchronisation, i call finish() function on my Activity.
Will I then return the onCreate(), or straight to onResume() of the main Activity?
Does this implementation make sense?
You can continue doing the same but for the first activity where you do the synchronization thing, make it a noHistory Task. http://developer.android.com/guide/topics/manifest/activity-element.html#nohist
Or in your First Activity, after you call startActivity(MainActivity) you can call finish() and by doing which your FirstActivity will be removed from backstack and then MainActivity will remain on top of the stack.
Hope that helps.
UPDATE
I mean to say is, let FirstActivity be your first activity and you start MainActivity from FirstActivity. After you call startActivity() in your FirstActivity, call finish() in the very next statement. This is completely acceptable.
UPDATED
to prevent your first Activity from being viewed again, you just need to add the following line to you Activity declaration
<activity
android:name=".FirstActivity"
android:noHistory="true" />
Using the noHistory tag will remove your Activity without the need to do it your self programmatically

How can I launch an activity to be on top of all other activities?

I am trying to create an application that defines two activites. The first activity pretty much runs all of the time. The second activity requires the user to authenticate to use the device.
Most of the time this application works fine. However, I am having problems figuring out how to force the second activity to the top of the window stack. The code calls startActivity passing in an intent to start the second activity. The problem is that when another application is running (e.g. a web browser), the second activity is not on top. When the other application exits, the second activity is visible to take input from the user.
Here is the activity definition for the second activity that I want to have always on top when started:
<activity android:name=".Authenticate"
android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden">
</activity>
It is my understanding that when start activity is called, it will put the new activity on top. Is there something that I am missing? How can I make the authentication activity come to the top when it is started?
Well AFAIK, you cannot force your Activity to stay on top all the time. If some other process (say Web Browser) creates an Activity after you have created yours, then that processes Activity will have focus and not yours.
whenever a new activity starts it is on top of the stack and if another application is running then it will be on the top of the stack and if you want your activity on top of the stack then you should stop other application being launched....Is there something I am missing then let me know..

How to start an activity from a singleInstance activity?

I have a widget which can pop up small dialogs when clicked. These dialogs are displayed by an activity called RemoteActivity in singleInstance launchMode. In one of these dialogs, there is a button to launch the main app MainActivity, which has the standard launchMode.
However, when this button is clicked, and startActivity() called, MainActivity isn't launched, although I can see the corresponding "Starting activity: Intent { ... }" in logcat.
If I set the launchMode of RemoteActivity to standard then MainActivity gets launched, but this isn't what I want, RemoteActivity is merely an extension of the widget, I don't want it to stack with any other activity.
I also tried with FLAG_ACTIVITY_NEW_TASK but it didn't help, and it shouldn't be necessary anyway according to the docs:
A "singleInstance" activity, on the
other hand, permits no other
activities to be part of its task.
It's the only activity in the task. If
it starts another activity, that
activity is assigned to a different
task — as if FLAG_ACTIVITY_NEW_TASK
was in the intent.
How can I launch my main activity?
UPDATE / ERRATA:
The MainActivity is actually launched but only if it isn't already part of a task. If I launch MainActivity normally through the launcher, and press Back to exit, then RemoteActivity does launch MainActivity.
But if, instead of pressing Back, I press Home to leave MainActivity, then RemoteActivity can't launch MainActivity, although the intent appears in logcat.
I'm testing this on Froyo.
Any idea of what's happening?
Maybe the noHistory flag will work for what you are looking for?
I found the problem: this behavior only occurs when calling finish() before startActivity() in RemoteActivity. If I call startActivity() before finish() then it works fine whether MainActivity is already part of an existing task or not.
Go figure...

Categories

Resources