How to have second launcher always open the application at specified activity - android

In my application, I have specified a second activity that can be launched from the launcher, using this manifest entry:
<activity
android:name=".Lists.ListOfListsActivity"
android:icon="#drawable/ic_launcher_lists"
android:launchMode="singleTop"
android:label="#string/lists_activity_name" >
<!-- An Intent filter so that the Lists activity shows in the Launcher -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Let's say I have the app open at the "main" activity then press the home key. My app will still be running, but in background.
Later the user selects the launcher icon I have for "ListsOfListsActivity" from the homescreen.
This will bring the application to the foreground, but NOT at the "ListOfListsActivity", but at whatever it's current activity was when it went to the background (e.g. at the "main"activity).
This is confusing, as the user selected the "ListOfListsActivity" but is shown another one. Then they have to navigate to it.
I had this working better, by specifying launchMode = "singleTask" for the "ListOfListsActivity", but in that mode it cannot be launched from another activity for a result (startActivityForResult() ), and I need to be able to do that to pick a list...
Question:
- how to specify an intent-filter that will force an activity to the foreground and be the selected activity, no matter what the current status of the application and it's current activity??

My final implementation was to define a different taskAfinity string for each activity I wanted to launch independently from the Launcher.
That way, each "shortcut" always launches the activity I want, but the downside that I have not been able to avoid is that the user may have multiple tasks with an activity from my application in it, and maybe the same activity open/active in different tasks....

Related

Push notification to open specific activity

Consider 3 activities:
(A) Login Activity (main launcher)
(B) Main Activity
(C) Specific Activity
After users logged in (A) they will access (B), and from, and ONLY from (B), they can access (C).
Now everytime the app is opened, it will first launch (A) and then (B).
I want to make a push notification where when clicked, it can access (C) but must start (B) first.
I am using Xamarin.Android + Appcenter Push Notification
I can get notifications both when my app is in foreground and background.
My problem is when my app is in the background, clicking at the received notifications in the status bar causes it to relaunch the app, starting from (A).
I need help with skipping (A) since user is already logged in, opens (B) and THEN opens (C)
Any ideas? Hoping this is not too confusing for you guys.
I have also tried setting launchMode=singleInstance to (A) and (B) but it still relaunch the app
You cannot change the launcher activity dynamically, but there are a few workarounds you can try:
Create a transparent activity and make it as the Launcher activity:
<activity
android:name=".ActivityLauncher"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
And select the next activity in it's onCreate()
if ( logged() ) {
intent = new Intent(this,ActivityB.class);
} else {
intent = new Intent(this,ActivityA.class);
}
startActivity(intent);
finish();
You can achieve this send a activity name or some value to select your activity. Depending on the value that u are sending you can launch the activity and also add the activity in backstack whenever u go back fron notified activity the page in backstack will appear.
I do always like this, hope this will work for you.

Android managing task about single task and task affinity

After looking into the codes i need to maintain/modify and the different documents and explanation, i really got confused single task and task affinity.
https://developer.android.com/guide/topics/manifest/activity-element.html
https://www.slideshare.net/RanNachmany/manipulating-android-tasks-and-back-stack
https://developer.android.com/guide/components/activities/tasks-and-back-stack.html
What i understand is activity launch mode singleTask will create a new task and put that activity at the root of the task. "singleTask" and "singleInstance", should be used only when the activity has an ACTION_MAIN and a CATEGORY_LAUNCHER filter
activity taskAffinity is the task that the activity has an affinity for. By default all the activities share the same affinity, the default package name. If want to have the activity different affinity task, then define another name and launch with FLAG_NEW_ACTIVITY_TASK.
In my maintain code(not the original developer), there have so many activities in AndroidManifest.xml with launchMode singleTask.
<activity name="LauncherActivity"
launchMode="singleTask">
<intent-filter>
<action name="action.MAIN" />
<category name="category.LAUNCHER />
</intent-filter>
</activity>
<activity name="TabsActivity"
launchMode="singleTask">
</activity>
<activity name="SubTab1"
launchMode="singleTask">
</activity>
<activity name="SubTab2"
launchMode="singleTask">
</activity>
<activity name="SubTab3"
launchMode="singleTask">
</activity>
<activity name="SubTab4"
launchMode="singleTask">
</activity>
<activity name="ScreenActivity"
launchMode="singleTask"
taskAffinity="com.xxx.xxx.sa"
excludeFromRecents="true">
</activity>
When click on user icon,
it will launch the LauncherActivity. LauncherActivity do some tasks for checking and launch TabsActivity and finish
itself(LauncherActivity).
TabsActivity add Tabs (SubTab1, SubTab2, SubTab3, SubTab4).
ScreenActivity will be launched startActivity with
FLAG_ACTIVITY_NEW_TASK.
ScreenActivity also put a notification icon so that the user can directly back to this activity.
SubTab can launch the other standard launchMode which will be destroyed after use back key.
EveryTime user click on App icon, it will take to TabsActivity instead of the last activity the user interact. So I add some code in the LauncherActivity onCreate()
if (!isTaskRoot() && !TextUtils.isEmpty(action)) {
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) &&
Intent.ACTION_MAIN.equals(action)) {
finish();
return;
}
}
This successfully bring to the last activity that the user interact.
The first question comes to me that isTaskRoot(). According to the explanation:
boolean isTaskRoot ()
Return whether this activity is the root of a task. The root is the first activity in a task.
LauncherActivity defined as singleTask as mentioned above, so it should be the root in the task. But when onCreate() check that, it is not the root task when there is other activity is launched including SingleTask activity. When TabsActivity is launched, when click on AppIcon, LauncherActivity onCreate() isTaskRoot() return false.
LauncherActivity and TabsActivity, they should be in different tasks and in each task, each would be the root activity.
In some other comments, it mentioned that isTaskRoot() is true when there are no activities in activity stack. So if there is activity in the stack, if will return false. So it is more related to the stack instead of task? I really get confused. Can someone explain about why it is different with document explanation.
The second modification is that I removed the ScreenActivity taskAffinity. In the old design, ScreenActivity can be be backed to through clicking on notification. But there is new request that if there is ScreenActivity last launched by user, when click on App icon, it should back to that activity. After removed taskAffinity in AndroidManifest, it does happen what i want.
I am looking this slide share tutorial. But still not get that.
https://www.slideshare.net/rajdeep/managing-activity-backstack
My question is that if the singleTask without taskAffinity, how they create a new task for each different singleTask activity. How the backstack works?
After modification, LauncherActivity, TabsActivity, ScreenActivity are SingleTask with the same default package name task. So they are in the same task stacked? If yes, it is not the same with document explanation. Please help me to get more clear of that.
Thanks a lot.

App is getting started again after launcher click even when it is already running

I have the problem that my App is starting again if the user clicks the launcher icon even if it is already running. I used singleTaks for that Activity and as many Stackoverflow Answers said -> This should solve the problem - but it doesnt.
<activity
android:name=".activities.ActivitySplash"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
As you can see i did define the launchMode to be singleTask. I open the App. Navigate to Screen X (I even could stay on the Startscreen, has the same effect) and now i press the home button. When i now press the launcher icon again the already running app does not pop up or is getting resumed (call it as you wish). Instead the App is completly started new.
How can i prevent that behaviour, if singleTask isnt working?
Try with SingleTop:
if the target task already has an existing instance of the activity
at the top of its stack, that instance will receive the new intent (in
an onNewIntent() call); a new instance is not created. 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.

Activity and Flags Intent [duplicate]

I have a Notification which starts an Activity. After a long press on home button and selecting my app, I want to start my main Activity again, and not this Activity started by the Notification. I tried with FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS, but this removed my whole application from the recents, and that's not what I want to achieve. How can I have my app in the recents, but have the main Activity started?
Regards
Okay, I found the solution to my problem. I started an Activity from a Notification with FLAG_ACTIVITY_NEW_TASK. But it seems to me that this Activity only gets started in an own task if affinity is different from the default affinity. So I had to add a different affinity in the manifest.
And it seems that FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS does not (as documented) exlucde the Activity from the recents, rather it excludes the whole task (not the whole application) in which the Activity gets started from the recents. And as I hadn't set a different affinity the Activity which I wanted to exclude was started in the same task (although I had set FLAG_ACTIVITY_NEW_TASK) and so my whole application (as it was running in only one task) was excluded from the recents.
Now I've set a different affinity for the Activity that gets started from the Notification and I start it with FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS. When I leave this Activity and long-press the HOME button I can choose my app and the default task is started or brought to the front.
If it's wrong what I mentioned above, feel free to clear it up ...
It's not clear to me what you want.
"How can I have my app in the recents, but have the main Activity
started?"
If you want to always start one activity using the long home-press, you can define your activity as singleTask in the manifest.
That way, when you select the shortcut in the long press HOME, it will always show the MAIN, singleTask activity. I say this because I used this behavior before once. ;-)
I believe that by using this you can still start an activity from the notification normally, using, say, Intents.
In the activity tag:
android:launchMode="singleTask"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Share from browser on the second try does not invoke my activity

I have a dictionary-type application that accepts, via an intent filter, arbitrary text shared from other apps, e. g. the browser. The intent filter is attached to a decicated activity ("Lookup") that does some posttranslation of the shared text and invokes my main activity. The lookup activity quietly closes.
I'm testing it with the system browser on Samsung Galaxy with Android 4.0.4, and here's a funny thing I notice. On the first try, it works as expected. I tap "Share", select my app as the target, the lookup activity is invoked, then the main activity is invoked. Now, if I task-switch back to the browser and hit "Share" again on a different piece of text, the menu of sharing targets is not displayed. Instead, my main activity is resumed. Not the lookup one (that'd be reasonable), but the main one. onNewIntent() is not delivered to neither main activity nor Lookup activity.
onResume() is delivered to main, though.
However, if I tap "Back" instead of task-switching back to the browser - that is, explicitly close my main activity - on the next try, the Share operation displays the menu of targets again.
What's up with this behavior? Does the Share functionality remember the target of the last share operation and try to reuse it? If so, why won't it remember the immediate target of the share - my lookup activity? Is this documented?
EDIT: the manifest for the lookup activity goes like this:
<activity android:name=".Lookup"
android:label="#string/IDS_LOOKUPTITLE"
android:theme="#style/Theme.Transparent">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
When I start main activity from lookup activity, I'm trying to reuse an existing one (if any), so the flags are FLAG_ACTIVITY_REORDER_TO_FRONT|FLAG_ACTIVITY_SINGLE_TOP.

Categories

Resources