Stopping multiple activities from showing up - android

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"

Related

Check For Activity is onScreen when Notification is Clicked ?(using Intent)

I am trying to add upload image feature in my application, using android-upload-service.
I am facing a issue not specific to the library.
When i am clicking on upload notification I want to open a activity using Intent.
code:
val intent = Intent(context, EditorActivity::class.java)
intent.putExtra("ResourceId", resourceId)//No I18N
context.startActivity(intent)
But,
First requirement is to start activity if it's not onScreen.(i.e application might be dead, or i might be on different activity)
Second if the activity is already running then do nothing
How can i check is the specific activity is dead or running and on Screen.
current code is starting the activity on top of present activity because of what i am having same activity on top of each other.
Add launchMode="singleTop" to the <activity> definition in the manifest. That should be enough to do what you want. It tells Android that it should use an existing instance of the Activity instead of creating a new one, if an instance of that Activity already exists at the top of the activity stack in the task.
Please don't use one of the special launch modes singleTask or singleInstance as these usually create more problems than they solve if you do not know exactly what you are doing.
First, add android:launchMode="singleTask" for that activity in your AndroidManifest.xml file. This will make sure that there is only one instance of the activity at all time.
Example,
<activity
android:name=".activities.MyActivity"
android:launchMode="singleTask"/>

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.

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.

Finish leaves activity in stack

I have an activity that performs a small bit of code and then calls finish. This works fine and the activity goes away, but if I press the history button (if it's called the history button?) the activity still appears to be there, though clicking it will do nothing.
I have enabled android:noHistory="true" in my manifest. I only have a single activity that changes some settings, and then calls finish, nothing else.
Is there a way for me to make that trace of the activity go away?
Assuming that the history button you refer to is the list of recent apps, you can set
android:excludeFromRecents="true"
By default value for this attribute is false.
For other attributes check the link: http://developer.android.com/guide/topics/manifest/activity-element.html

Android history only for 1 activity

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

Categories

Resources