I added singleTask attribute to MainActivity(A) to avoid loading the activity multiple times. After the other activity(B) is on MainActivity(A->B), if I go back to home screen by pressing home button and re-launch the application, there is no B (A->B->HOME->A)
There IS an answer in here, saying to add FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP every time calling A Activity.
BUT, I have a Intent data scheme in Manifest file, and the application should be launched by the scheme.
Please, help me...
It sounds to me like Android is launching your MainActivity(A) again after you return to the HOME screen. This shouldn't happen, but is a long-standing nasty Android bug. See my answer to this question for more information.
Related
I have an app that runs a service (audio player) in the background and displays an ongoing notification while it is run. When the user clicks the notification, I'd like to open the player activity without it showing up in the recents list and without bringing the rest of the app to front, so when the user presses the back button they go directly to the app they were using previously, regardless of everything. I tried three different ways and none of them worked well.
Start an intent with FLAG_ACTIVITY_NEW_TASK. If there is an existing task, it's brought to the front. When the user presses the back button, the player activity finishes and reveals the previous activity in the said task instead of going back to the app that had been in foreground the moment the notification was clicked.
Start an intent with the following flags: FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_MULTIPLE_TASK | FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS. The existing task isn't brought to the front and the back button works as expected. However, after this activity finishes, the app disappears from the recents list completely. The only way to get it back is to start it from the launcher icon which brings that existing task to the foreground as if opened from recents.
Start an intent with the following flags: FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_MULTIPLE_TASK | FLAG_ACTIVITY_NO_HISTORY. The activity starts in a new task, but said task replaces the main one in the recents list. Adding FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS makes it behave exactly as in previous case.
Is there any proper way to do such navigation, or should I give up trying and just make it bring an entire existing task to the front with the player activity launched on top of it, as in case 1?
Is it convenience for u to declare the launchMode to "singleInstance" inside this Activity Tag in the Manifest File? I think that may help u.but i am not sure.U can try
Maybe you can try launchmode as a tag for your notificationActivity in your manifest;
android:launchMode="singleInstance"
you should try new document-centric API:
Adding Tasks to the Recents Screen
I actually figured out the proper way to do this: add the taskAffinity attribute to your <activity> in the manifest. This way, it won't interfere at all with how the rest of the app displays in recents, exactly what I was trying to achieve.
I am launching an activity A from a service in the background, this works well and the activity is created and usable with the code below:
Intent intent = new Intent(context, getActivityClass());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
This however has an unwanted behaviour if there is already a running activity B of the app containing activity A in the task list.
The user would expect to get back to the activity that was in the front when my activity A was started from the background if he uses the back button,
however it navigates back to activity B instead which really is confusing.
After reading docs I tried to allow task reparenting which did not work and still shows the problem described above.
<activity
android:name="net.x.y.z.PickContactActivity"
android:allowTaskReparenting="true"
android:theme="#android:style/Theme.Translucent.NoTitleBar">
</activity>
According to this question Need single activity in new task backstack the Intent.FLAG_ACTIVITY_MULTIPLE_TASK should be used, however it is mentioned in the comments on this to not use it unless implementing an app-launcher
Do not use this flag unless you are implementing your own top-level application launcher. Because the default system does not include graphical task management, you should not use this flag unless you provide some way for a user to return back to the tasks you have launched.
What do I then have to do to fix this? In short: the pick contact activity should simply be handled as a real new task without being associated with any other running instance of the app, just as i would open notepad two times on a windows pc
I'd really be glad to have good solution on this,
thanks in advance
BY DEFAULT;two activities from the same app will appear in the same task;to change this behaviour add this in the Manifest xml for the activities A and B: android:taskAffinity="";and change android:allowTaskReparenting to "false"
I have program with login, main activity and other activities.
First step is login activity(A). If login succeed start main activity(B) and call finish for (A).
working with (B) I'm calling some activities and then back to (B).
When decide to exit - I call logout and try to close (B) calling finish.
This logik works in 70% ot time :(
Unfortunately on 30% after calling finish for (B) - activity(A) appears on screen and start logging me.
Who is starting (B) again? I din't see relation between problem and program usage.
Update:
I put hohistory for (B) and start (B) with FLAG_ACTIVITY_CLEAR_TOP.
UPDATE2: Described behaviour is typical when I set screen orientation mode in code. In manifest is set portrait. When start activity I'm setting orientation depending on user config. This produced onCreaste twice. I got managed to handle this properly, but this causes problem as described. If I don't set orientation - one onCreate is called and no problem with finish.
Check the following links, you will get the solution:
Finish parent and current activity in Android
http://developer.android.com/guide/topics/manifest/activity-element.html#clear).You
You can try this when you logout in activity B. The activity displays the home screen.
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
Are you starting your application from an IDE (like eclipse), or from the app installer? If so, you will see this situation if you do the following.
Launch your application from the IDE or after installation from the installer (displays your first activity)
press the HOME key (takes you back to home screen)
launch your application again by selecting it from the list of available applications
This sequence will create 2 copies of your first activity, one on top of the other. When you finish the top one, the one underneath it will be shown.
You say this doesn't happen all the time. If you don't launch the app from the IDE, but just from the list of available applications you won't see this behaviour. Also, if you never press the HOME key and relaunch the app using the list of available applications you won't see this behaviour either.
When you go from activity A to activity B first time you are supposed to clear your stack top. Otherwise activity A stays in the stack below B and when u finish B, activity B is called again.
you should set in manifest file android:noHistory="true" to make your A activity not to stay on android stack.
In case you would like to do it from code in future by using intents Intent.FLAG_ACTIVITY_NO_HISTORY will do the job for you.. Cheers
Using setScreenOrientation made thinks complicated.
It is not enought to set noHistory for activityA and call activityB with FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_NEW_TASK.
When Start app in portrait mode /as declared in manifest/ - It was OK.
But when call setScreenOrientation - have to call finishB 2 times to exit.
/Probably because ot 2 times onCreate for activityB/.
This made thinks to work:
For activityA: android:noHistory="true" and android:launchMode="singleInstance" in manifest.
Start activityB with startActivityForResult and flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_NEW_TASK.
In activityA:
1. startActivityForResult(activityB)
2. finishA.
In activityB - when call finish() for B - because of 'singleinstance' system din't start activityA again.
Hope this help.
If anybody know reason which will cause error - please write me.
The problem is somewhat odd and after having trying to figure it out for about a day now, I am posting it here.
I have an application where an activity A(main activity) launches other activities(B,C or D).
The issue here happens when activity A has started Activity B and 'home' button is pressed.
Case 1 - When I test my application in debug mode on my device (HTC Desire) after pressing the 'home' button, I again click the application icon, it returns to the same activity (activity B), which is what is should do. No issues here.
Case 2 - When I export the signed package, and then install the application on the same device, then if I click the application icon after pressing the 'home' button, then a new instance of activity A (main activity) is launched ON TOP of activity B. I got to know this because when I press 'back' from that activity, it returns to activity B and pressing 'back' again shown activity A.
The behavior ceases to exist if the application is quit in the same order it was started, that is, if I press 'back' from activity B, then 'back' from activity A (exit).
After this everything runs fine.
I have tested it many times with different settings but I can't seem to figure out why the behavior is like this.
Any help is appreciated.
I think giving Activity A the 'single top' flag in your manifest should fix this.
Regarding Case 1:
When launching your intent from Activity A to start Activity B, add the flag FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
This will ensure that when you go home and launch the the app again, Activity A will be shown.
Regarding Case 2:
I'm not exactly sure how this would occur. It seems like it thinks you have two versions of the app, the signed one and the unsigned one but keeps them both in the same task stack. You may want to consider using singleTask or singleInstance for your Activity if you only want one instance. See the doc on tasks and back stack for more details.
I would agree with Noel regarding the likely cause of Case 2. Without task reparenting or it being set to a launchmode preventing multiple instances of an activity, there is a chance that launching it from Home isn't deemed the same stack as launching it from Eclipse (assuming this to be the case).
In my talent calculator app I have the whole application set allowTaskReparenting=true to ensure nothing is left in other stacks (primarily email as it can email launch urls). I then have my main activity set to launchMode="singleTask" as I only ever want one instance of this to exist no matter what launches it or with whatever intent.
My only other activity is for loading and saving and that has noHistory="true" to make sure it is removed and never returned to. That basically means it only exists while you're in it, and can never return to it.
clearTaskOnLaunch="true" will also ensure only the main Activity remains in the stack when it's launched from Home, but this isn't always the case if you have other ways to get into your activity. If it's only ever launched from Home then set this.
Hope that all helps.
Do you start you application manually or using Eclipse or another IDE? When starting from Intellij IDEA I had exactly the same problems. Then I stopped and ran it manually and behaviour was as expected.
I'm experiencing kind of strange behavior of my application after hard Home button is pressed.
When you press Home, everything is OK - my app goes to the background, showing Home screen. But if you try to choose my app in the main menu or in the list of last tasks it behaves like it was not started before and does not show the last activity you were on - it just starts from scratch, namely, shows the splash screen and starts next corresponding activities. Moreover, old activities of this app remain on the activities stack, and previous instance of the app is not terminated - so if you press Back for a few times you'll just run into those activities which were undoubtedly started during the previous session of work with my app. Splash screen activity is filtered by "android.intent.action.MAIN" filter and "android.intent.category.LAUNCHER" category.
The strange thing is that all of that happens despite the fact that I do not intercept any Back key hits, or override any onPause or onResume methods. What's happening contradicts with my understanding of Android app lifecycle - I was sure that when you hit Home an app just goes to the background, and when you choose it in the menu later - it just unwinds and does not start anew. (Of course, unless stuff like that is stated in the app manifest or corresponding methods are overridden or something else).
I also checked it for some other lifecycle events - such as changing orientation or flipping hard keyboard out - and none of those led to such strange results. It appears that the problem occurs when you try to start the app from main menu or menu of last applications.
I hope you will be able to help me. Any advice on what to pay attention to or where to search for solution would be really great.
Regards, Alex
You need to set android:launchMode="singleTask" in your LAUNCHER activity in your manifest file.
For more info on the launchMode attribute see here
Note that:
The default mode is "standard".
and:
Every
time there's new intent for a
"standard" activity, a new instance of
the class is created to respond to
that intent.