Back pressed or finish() goes to the wrong task in Android - android

From a Service, I launch an Activity in my application. This is not the main application activity, just some random other activity.
After the user has interacted with this Activity, I want to close it and return the user to where he was. So say for example he was in Facebook. My Activity gets launched. When he is done interacting with it, I want to return the user to Facebook. However, when I call finish() or backPressed() or anything like that, the Main (launcher) Activity of my application is started. I don't understand why.

Related

Android crash on one activity delegates to previous activity

I have two activities, first one is LoginActivity, the second one is MainActivity. When an error occurs on MainActivity I get force close dialog which is ok, but why does my app goes to previous activity and runs some code on it and then I get another force close?
Shouldn't app close immediately when error occurs on particular activity, instead of after crashing on MainActivity I see in log that it executes some code on LoginActivity as well and if not not properly handled, it crashes there too so whenever I get an error on MainActivity it triggers one more force close dialog on LoginActivity?
What do you expect the user experience to be? What if they press the back key?
When an Activity crashes the app opens the last Activity that was running as it's at the top of the activity stack. So what you're experiencing is expected behaviour. But you weren't expecting it so it seems you may have structured the flow of your app incorrectly.
When the user starts your app they should be presented with the main activity, when they try to log in they should be presented with the login activity, once login is complete the login activity should be finished so it does not remain in the activity stack.
If you do not finish the login activity and instead start the main activity then the user will be able to press the back key to return to the login activity which is not ideal, as they are already logged in.

Start activity of another task and stay within current task

I have an application with, let say, two activities. One of the activity is already launched and after pressing "home" button it is in background. Now, from the ANOTHER application the second activity is launched. And when this second activity is finished I got to the first launched activity, not in the application that started the second activity. Is it normal behaviour? If it is, is there some way to change it? I need to stay in the application that launches the second activity, and not in the application which this second activity from.

Calling finish() in onPause in child activity so that user refocuses into parent activity. Child activity gets recreated instead

I create a child activity "B" from activity "A". if the user should leave the app for any reason (most likely hitting the home button), I would like activity "B" to end and the app to be at activity "A" once the user resumes.
If I call finish() manually, activity B ends and it returns to activity A. This is the behaviour I would like to happen when the user leaves the app.
I have tried to call finish() in the onPause(), onStop() and in the onUserLeavingHint() of activity B. In each case, this appears to work correctly, and I can see mParent.finishFromChild(this); being called inside activity B.
However, as soon as the user switches back to the app, the onCreate() of activity B gets called and the user ends up in activity B.
How can I ensure I end up in the parent activity when I call finish() from within an onStop() (or similar) handler?
UPDATE: It appears that the issue is related to activity B being declared as using a SingleInstance launch mode. Removing this feature seems to have resolved the issue. Changing this has introduced other issues that I have since managed to fix.
The reason for this happening is that Activity B is set as a SingleInstance Launch Mode. The reason it was set to this (by another developer) is somewhat related to the reason I had wished the activity was ended when the app is in the background - it was to ensure the user could not reach this activity by hitting back on any other activities subsequently dispatched from Activity B.
To resolve this. I first ensured no activities could be created from B. To instead return from B and pass any required Intents on to A. Simplifying the back stack. (Calling activity B with startActivityForResult() is one possible way of doing this.)
Now, the reason SingleInstance causes this issue to arise in this scenario, is because Activity B is launched in a seperate new task. When the user attempts to resume, they re-enter this single-activity task. The rest of the app is running in a seperate task. The only thing the task can reasonably be expected to do is relaunch the activity. When the user presses back, the only thing it can do from there is to close the task (and hence appear to exit the app). For the expected behaviour to occur the user would have had to have selected the other, first task (through a long click of the task list).
Hopefully this self-answer can help someone who has encountered a similar issue.

Changing the main launcher activity programmatically

My app works as launcher. 10 mins later another activity starts automatically. When this activity started, if user presses home button, he returns to the main activity. However, I want to change the launcher activity as the second one. It must be forbidden to return to the main activity even if he presses home button.
Instead of playing around with the intent filters, I suggest you create a blank Activity, with no UI, and register it as your launcher.
This Activity's sole role is to choose the correct actual Activity you wanna show, launch it as a new task, and then quit silently.

How to finish() every Activity on the stack associated with the Application?

Is there any way to tell Android, "If the user exits this Activity by hitting the 'home' key, immediately finish() every Activity on the stack that has ever been associated with this Application?" Or, alternatively, a way for an Activity whose onPause() method has been forcibly called by a user hitting 'home' to finish() not only itself, but the Activity that started it as well?
The problem I have is workflow. My application's users basically go back and forth between a main Activity and "detail" Activity windows. They see something in the main Activity, tap it, the child detail Activity gets spawned, they use it for a while, then hit back to return to the main Activity. I can't finish() the main Activity after starting the child Activity, because then the back button would quit working. The problem is that now, if the user exits by hitting the home key in a child Activity, it leaves behind a zombie parent Activity. If the user relaunches the application, uses it for a while, then explicitly triggers a shutdown by hitting menu->quit from the main Activity (where I kill the service, then call finish()), the Activity goes away, and the user gets dumped into the zombie Activity from the previous run (which promptly crashes if the user tries to do anything, because the background service it depends on has already been shut down, and all the BroadcastReceivers that would have otherwise been listening have been removed, so its cries for help go unheard).
I'm actually kind of puzzled, because I already have android:launchMode="singleTask" sitting in the manifest tag for my launch activity, and I thought launchMode="singleTask" was explicitly supposed to prevent things like this from happening by making sure that any lingering Activities from a previous run were safely dead before it relaunched.
The easiest way of doing this is:
Intent i =new Intent(CurrentClass.this,HomeClass.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
Perhaps you can mark all the Activities in the stack with clearTaskOnLaunch (see http://developer.android.com/guide/topics/manifest/activity-element.html#clear).You can mark all the Activities that would be in the task with the attribute.

Categories

Resources