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

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"/>

Related

Where should I add android:taskAffinity when I launch existing instance of an activity from static shortcut in Android Studio?

As the static shortcuts will always have FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK set,
I've created TempActivity which is an invisible Activity i.e It'll start MainActivity and then calls finish(). And also as suggested in developer docs SecondActivity has android:taskAffinity="" in the app's AndroidManifest.xml file.
I have read Manage shortcuts and How to launch an existing instance of activity from static shortcut.
You can see Image A and Image B. There are different opinions.
Where should I add android:taskAffinity when I launch an existing instance of activity from the static shortcut in Android Studio? MainActivity or TempActivity?
Image A
Image B
It should be on TrampolineActivity of course.
But the SO question you've provided has another error which makes it work eventually.
If you read the docs carefully, you can see the second bullet saying
In the shortcuts resource file, the intent within the static shortcut should reference the trampoline activity
But the question from SO has wrongly set it on MainActivity's meta-data

Launching an Activity from the background without attaching it to the task stack of existing activities of the same app

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"

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"

How to open already opened activity instead of creating new one?

I have two activities with "navigation menu" which has items for launching Activity1 and Activity2.
For example we starts Activity2 from Activity1 and then we want open Activity1 by tap on "navigation menu", but when we do this we get new instance of Activity1 instead of open еxisting instance.
How can i open instance of Activity1 if it already exists and create new instance if not?
Add FLAG_ACTIVITY_REORDER_TO_FRONT to your Intent you use with startActivity().
add android:launchMode="singleTop" to your activity in the Manifest.xml
<activity android:name=".myActivity" android:label="#string/app_name"
android:launchMode="singleTop" />
Check this out about different launchModes
also mind this:
As shown in the table above, standard is the default mode and is
appropriate for most types of activities. SingleTop is also a common
and useful launch mode for many types of activities. The other modes —
singleTask and singleInstance — are not appropriate for most
applications, since they result in an interaction model that is likely
to be unfamiliar to users and is very different from most other
applications
Set the flag of the activity to singleTask and override the onNewIntent(Intent intent) to catch the new intent.
The most complete answer would be to use android:launchMode="singleTask" and depending on your functionality, override onNewIntent since it will be called if there is already an instance of the Activity with the new Intent passed to it.
<activity
android:name=".MainActivity"
android:launchMode="singleTask"/>
Why?
Based on the question.
There are two Activities, Activity1 & Activity2
We open Activity1 and then from Activity1 we open Activity2. Then, inside Activity2:
How can i open instance of Activity1 if it already exists and create new instance if not?
As stated in AndroidManifestActivity_launchMode for singleTask
If, when starting the activity, there is already a task running that starts with this activity, then instead of starting a new instance the current task is brought to the front. The existing instance will receive a call to Activity.onNewIntent() with the...
Furthermore, under the intent class, if you read about the singleTask launchMode it already uses Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT and manually setting an intent to it is not normally set by application code.
As stated in FLAG_ACTIVITY_BROUGHT_TO_FRONT
int FLAG_ACTIVITY_BROUGHT_TO_FRONT
This flag is not normally set by application code, but set for you by the system as described in the launchMode documentation for the singleTask mode.
Therefore, by using singleTask launchMode you ensure that there is only one instance of your application and you do not have to be adding the FLAG_ACTIVITY_BROUGHT_TO_FRONT flag to your intents in every activity which calls your Activity2 as suggested by CommonsWare.
Now, if we use the android:launchMode="singleTop" as weakwire suggested, the link he provided himself clearly states;
"singleTop"...In other circumstances — for example, if an existing instance of the "singleTop" activity is in the target task, but not at the top of the stack, or if it's at the top of a stack, but not in the target task — a new instance would be created and pushed on the stack.
In other words, we would end up with multiple instances of Activity1 in some scenarios which is what we do not want.
As final say, if you are one of those who love editing answers that contribute nothing to the answer itself, go answer some real questions if you really want to increase your Stack Overflow Reputation.

Intent filter detection in several Activities

I am developing an NFC application (although I think this doesn't really matter for my problem) where I currently have a MainActivity with a TabHost and some other activities (one per tab). I also have an Activity that I have done to read NFC tags. This activity contains intent filters in the manifest to catch the tags.
Right now if I scan a tag the last activity runs and launches a dialog. The problem is that it is an independent activity and the background is empty. I would like to launch the dialog in the current activity instead of launching a new one.
How can I do this? My first thought has been to extend the NFCReader activity in the other Activities but it doesn't work. I think the problem is that in the manifest I have specified the intent-filter only for the NFCReader activity, not for the rest, but I'm not sure.
So the question is: how can I use an intent-filter for all the activities of the application and launch a dialog in front of the current activity without starting a new one?
Move the NFC handling to the activity that has the TabHost, in your case based on description I think it is MainActivity. I got NFC to work correctly on TabHost by that way in my case.

Categories

Resources