Activities back stack seems to be lost - android

I have 2 activities: A and B
In B, I have a back arrow to go back to A by calling "finish()" method.
It works fine, except when the following is done:
I go to B
I put my app in background
I restore my app from "recent apps"
I press the back arrow, and then, the app is finished instead of going back to activity A.
Any help please ?

I figured out how to solve this:
As #Karthikeyan mentioned in its comment, setting launchMode to "singleInstance" is cause of the problem. I changed it to "singleTask" and it worked fine.
In fact, according to the google doc stated in https://developer.android.com/guide/components/activities/tasks-and-back-stack,
"singleInstance".
Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance.
So logically, with "singleInstance", the activity when restored from the background had no other activity in the same back stack, and thus, calling finish() would simply finish the activity without restoring whatsoever (the very first activity is in the background and in another separate task)

Do not keep activities make sure that this option is not enabled in your device's developer settings.

Set flag in onStop () method to know and write condition onBackPress () condition to navigate to A according to the Flag value changes

Make Activity A as parent Activity of Activity B in your manifest file. Works for you.

Related

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.

Overriding onBack Pressed?

Correct me if i am wrong
"moveTaskToBack(false/true);" has nothing to do with the visibility of the Activity ,but has everything to do with Activity Stack,because many times on stackoverflow i find people being confused on this.
Now My Scenario:
there are two activites A and B
A is the root Activity
B is launched from activity A
the thing is i don't want my activity B to be killed after the back button is pressed,(just i want it to be invisible,and activity A to be visible which will happen on its own )so that i can restore its state afterwards.
so after searching a bit i came to know about
moveTaskToBack(false);,
which seemed to be the solution as it does not kill the activity (because the activity is not sent to back in the activity stack),but the only problem is.it works with Root Activity,and hence it will not work in my case.So is there any alternative which i can use with"non-root" activities,so that the state of Activity is restored....
I guess you have misunderstood
public boolean moveTaskToBack (boolean nonRoot)
Added in API level 1
Move the task containing this activity to the back of the activity stack. The activity's order within the task is unchanged.
Parameters
nonRoot If false then this only works if the activity is the root of a task; if true it will work for any activity in a task.
Returns
If the task was moved (or it was already at the back) true is returned, else false.
Back Button by default takes you back to the previous activity. It pop's the activity from the back stack and the previous activity in the stack takes focus.
Save the state of the activity in onPause restore it in onCreate or onResume. To store values persistently chekc the below link for storage options.
http://developer.android.com/guide/topics/data/data-storage.html
http://developer.android.com/guide/components/tasks-and-back-stack.html
You may want to check the answer by commonsware in the below link.
https://groups.google.com/forum/#!topic/android-developers/4Pz6LrzVpx0

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.

Android how to create activity only if it wasn't created yet

I have a problem cause when I go to different activities using startActivity function,
they always get created from scratch. Even if activity A was visited before, when I go to activity B and then A again, activity A is created again.
The problem is with back button, cause if I go to Activity A then B then A and then B,
in order to close the application I have to press back button 4 times.
I guess that it shouldn't act like it and user should be able to go to activity A when first pressed back button and the second press should close the application.
How to solve this issue?
Greetings
If you have activity transitions like:
Activity A -> Activity B
Activity B -> Activity A
and you want the user to go back to the same instance of Activity A in this case, maybe you just need to call finish() in Activity B after you call startActivity() for Activity A?
If this isn't helpful, please give us more information about what you are trying to do.
make sure you implement onSaveInstanceState and be prepared to restore your activity from a Bundle in onCreate. that's how you re-establish where you were when you return to an activity.
add launcheMode="singleTask" to your activity in the manifest
You need to set FLAG_ACTIVITY_SINGLE_TOP to your intent for launching activity A. Doing so will cause your previously created activity to re-use. Make sure you do handle your afterwards intents in onNewIntent method. For more info.
You need to set the flag FLAG_ACTIVITY_REORDER_TO_FRONT when you start activity A from B or vice versa, like
i = new Intent("....ActivityAorB");
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
I've tried solutions proposed so far, however they didn't do it for me.
What did however, is using flag FLAG_ACTIVITY_CLEAR_TOP while starting activities.
Thanks for pointing me in the right direction though.

Finish activity

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.

Categories

Resources