Android history only for 1 activity - android

I would like to know if it's possible to keep only 1 of each activity in history? I have tab-like interface and when user goes between activities it remembers them in history.
I tried noHistory attribute but it will cause activities to not go into history at all.
Ideally I'd like to have history but make it work like this:
Scenario:
Act1->Act2->Act1->Act2
In "standard" setup when clicking "back" it will unwind like
Act1->Act2->Act1->GONE
In "noHistory" mode it when click "back" it will be:
GONE
I want only 1 copy of each in stack, so it will be:
Act1->GONE
Possible?

When you call startActivity(), add FLAG_ACTIVITY_REORDER_TO_FRONT to the Intent. That will reuse your existing activity instance.

have a look at this:
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
I think you may want
android:launchMode="singleTop"
in your activity's manifest if I understand your needs correctly

Related

Activities back stack seems to be lost

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.

Customize back navigation

First of all I read this article but is kind of complicated for me . All I need is to go to an activity I want (navigate if exist in back stack or start it with Intent if it's not ) in onBackPressed() override . should I manage back stack
or something else ? if yes how ? and if no what is a simple way for that ?
P.S : I dont use ActionBar
you should override the second activity's onBackPressed() method and add android:launchMode="singleInstance" to your first activity in AndroidManifest.xml this will launch the first activity from backstack or create new if it doesn't exist in backstack.
Use startActivityForResult() wherever needed. Do not create new instances of the same activity. That is, if u want to go back to the previous activity, just call finish() from this activity.
Understand different launchmodes
As mentioned in the link above, singleInstance launchMode might be tricky and might cause issues.
SOLVED: as mentioned in this answer and according to comments , if we want to just bring an (existing )Activity to front , setting Intent flag to :
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
wont create new instance of Activity we want and just bring it front . this would be useful if we have multiple activities on top of the one .

Stopping multiple activities from showing up

I have it so when you click the notification icon on my app it opens up an activity. The thing is that if you click it over and over it opens the same activity over and over. So when you click the back button you have to go through all of those before going back to the beginning. How could I make it so that if that activity is already there it doesn't reopen it?
Check out the launchMode section of the manifest documentation.
You're looking to add:
android:launchMode="singleTop"
to the declaration for your Activity in the manifest. This means that if an instance of the activity is already at the top of the target task, the system will re-use that. Any intent gets delivered to the onNewIntent method of that activity.
I'd recommend having a good read of all the options on that page and deciding which one is right for your usage.
Try putting this in your manifest:
android:launchMode="singleTop"

Removing activity from recent apps keeping older one

I have this challenge with my Android APP:
I have and "dialog" - I mean an activity created as a dialog. Now, user presses the home button and leaves the app ~ later holds the home button and displays the Recent apps list. He will see the dialog as the last activity of my app. However, I don't want to display this activity on the "stack".
I tried the exceludeFromRecentApps (or similar name) and this one enabled removes whole app from the list. But I want to have dialog's parent activity displayed in the list.
I tried something with overriding the onResume, onPause and onStop methods, but no effect for me, since instance-level variables may be discarded (?).
I believe thats trivial problem, but I wasn't able to google working solution.
Lot of thanks!
android:noHistory="true"
android:taskAffinity=""
android:excludeFromRecents="true"
for your case, first you need to separate your dialog activity by "taskAffinity" tag. Then you need "excludeFromRecents" tag in you AndroidManifest.xml to remove is from recent list.
Like Hoan Nguyen said, you need to use the finish() method but know that the finish() method does not guarantee that the Activity will be finished.

Is there a way to set an android activities attributes conditionally?

In my Android application I have an activity where I've been using [Activity(NoHistory=true)] to keep the activity from appearing on the activity stack.
Now I'd like to make it conditional - sometimes it should be on the activity stack so the user can press Back to return to it from a subsequent screen; sometimes it shouldn't. Is there a way to "decorate" an activity conditionally or must I write some code to accomplish this?
Thanks in advance.
Are you looking for intent flags that can be set like the one below ? You could use that to start an activity and set the flag conditionally.
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Categories

Resources