Android launchMode="singleTask" and Intent-filters - android

So I have an Activity A that is defined in the AndroidManifest.xml as defined below:
<activity
android:name=".activity.A"
android:screenOrientation="landscape"
android:windowSoftInputMode="stateAlwaysHidden"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This Activity launches a welcome Screen which we will call Activity B.
If you launch the application the Welcome screen is displayed and then once the user is done with it Activity A comes back.
The issue I am having is when I push the "Home" button from the welcome screen I go back to the Home Screen as expected. Now when I reclick on the Application Icon the application won't launch. Instead both my Activity A & B get destroyed. If I click on the icon again then the application relaunches as expected.
Now if I'm on the welcome screen and push the back arrow and reclick on the App icon it launches the application as expected. I don't have to push it twice.
Unfortunately I have to use the launchMode="singleTask" since it is a requirement for integration with another team. I have read the Android API's for Tasks and Back Stacks numerous times. Any pointers or suggestions would be greatly appreciated.
I came across a blog indicating there is an undocumented bug with using singleTask and intent-filters together but didn't find any official documentation on this.
Thanks
EDIT
Launching Activity B like this:
Intent intent = new Intent(context, B.class);
startActivityForResult(intent, CONST_VAR);

I tried making two activities which launches ActivityB from Activity A. I see no such problem as described in the question. PFB my manifest. Also, when you say home button, is it Phone home button or your app specific home button. PFB my manifest
<activity
android:name="com.android.testsingletask.MainActivity"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:windowSoftInputMode="stateAlwaysHidden"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.android.testsingletask.WelcomeActivity"
android:screenOrientation="landscape"
android:windowSoftInputMode="stateAlwaysHidden"
android:label="#string/app_name" >
</activity>

Related

Difference between launching app from recents and home screen

I have ActivityA as launcher activity.
From ActivityA I open -> ActivityB. I put the app in background.
When I open the app from recents the app is resumed with ActivityB.
When I open the app from homescreen, the app is resumed with ActivityA without calling onCreate(), just onResume().
Why is ActivityB cleared from stack when I open the app from homescreen, even if onCreate() from ActivityA is never called, and how to fix this?
Manifest file looks like this:
ActivityA:
<activity-alias
android:name=".Launcher"
android:label="#string/app_name"
android:targetActivity="path.ActivityA">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
<activity
android:name=".path.ActivityA"
android:launchMode="singleTask"
android:screenOrientation="sensorPortrait"
android:theme="#style/BlackIntroTheme"
android:windowSoftInputMode="adjustPan">
<nav-graph android:value="#navigation/graph1" />
<nav-graph android:value="#navigation/graph2" />
<nav-graph android:value="#navigation/graph3" />
<nav-graph android:value="#navigation/graph4" />
</activity>
ActivityB:
<activity
android:name=".path.ActivityB"
android:launchMode="singleTask"
android:screenOrientation="sensorPortrait"
android:windowSoftInputMode="adjustPan" />
this is how Activity stacking works, some nice example of all launchModes in HERE, more complex info in DOCs
in short - Activity with singleTask started (again) will clear all on-top-of-it Activities. you have your first MAIN Activity declared with this launchMode, so every icon click on devices launcher will clear your Activities stack. you can track this by overriding onNewIntent method. picking from recents just brings all your Activities stack to front, with last opened on top ofc.
consider removing launchMode (is this necessary line for you?) or set it as standard (default)
android:launchMode="standard"

Android back button not working as intended after background

I have an activity A from which I start activity B.
If I press the back button it will go back to activity A, however if I press the home button or change to another app and come back to my app the back button closes the app instead, so I can no longer get back to activity A without restarting the app.
I use no flags when creating the intent.
Java:
Intent i = new Intent(this, InCallActivity.class);
startActivity(i);
XML:
<activity
android:name=".MainScreenActivity"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MANAGER" />
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity android:name=".InCallActivity"
android:screenOrientation="portrait"
android:launchMode="singleInstance"/>
Please try removing the attribute android:launchMode="singleInstance" in your activities, and check if it works.
Follow these links for more information: Activity Syntax and Launch Mode

After home button and returning, it does not return to previous activity

Activity A is a splash screen.
Activity B is a menu screen that is singleTask.
Activity C is a screen with noHistory that selects a photograph from the gallery with the built-in intent for choosing a photograph.
Activity D is a screen that manipulates photographs.
The problem that I have is that when I am in Activity D and select the home button, if I choose the application to be launched once more it returns me to Activity A to launch Activity B again instead of launching Activity D where I left off. If I use the recent apps however, it switches back to Activity D just fine as expected. So, there must be something slightly error prone in the way that I have it setup. But, I should expect it to return to Activity D in both cases so that the user can keep coming back to the Photo viewing screen (Activity D).
Thoughts?
<activity
android:name="ActivityA"
android:theme="#style/Theme.CustomDefaultStartup"
android:noHistory="true"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="sensorLandscape"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="ActivityB"
android:theme="#style/Theme.CustomDefault"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="#xml/device_filter" />
</activity>
<activity
android:name="ActivityC"
android:theme="#style/Theme.CustomDefault"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name">
</activity>
<activity
android:name="ActivityD"
android:theme="#style/Theme.CustomDefault"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name">
</activity>
Apparently it was the android:launchMode="singleTask" on Activity A that was causing the problems. Once, the singleTask from the splash screen was removed, the behavior returned as was expected. Not sure where the singleTask for the splash screen came from, but it was probably an error left in place for a while in the application.
Once that was removed, Activity D no longer launched the splash screen and the following menu after a home button press and return to the application. While I do not follow this completely as to why this would cause it in the lifecycle of events, this resolved the issue for me.

Purpose of using CATEGORY_HOME in android manifest?

What is the alternative to removing the following from AndroidManifest:
<activity
android:name="com.apper.main.UserActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I also found that after removing the above line, there was no impact to my android
application. What's the main use of this category and what's the alternative to it.
If the intent of this category is to launch the home screen then it can be done by the following:
Intent homeIntent= new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(homeIntent);
This above code will launch the home screen but why the line in the android manifest ?
What's the purpose as removing the line from here does nothing change the application ?
What's the main use of this category
It, coupled with ACTION_MAIN, identifies a replacement home screen.
what's the alternative to it
Not having it. Either you have this category, or you do not.
The category HOME is used to declare your application as a Home launcher. By putting this in the manifest then user will have the option to have your application open upon pressing the home button.
This is typically used when creating an application that will be used in a kiosk mode.
I do not believe there is an alternative to make an application a home launcher.
Documentation

android application not closing

I am in trouble, I want to start my application from starting every time but it's not being.
When I exit from my application & come again. I found same activity which I have left before exit.
Now If I **shut down or switch off** my Android Device direct when my application is in foreground & then I **switch on device again**. I get same activity which I have left earlier. But I want to fresh application from login page. Because my setter & getter is null after switch on device, and I found all value null in my application.
My Manifest file is below:
<activity
android:name=".Main"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Dialog" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".screens.ScreenSplash"
android:screenOrientation="portrait" />
<activity android:name=".screens.LoginActivity" />
<activity
android:name=".screens.LoginActivity"
android:configChanges="orientation|keyboardHidden" />
<activity
android:name=".screens.MainMenu"
android:configChanges="orientation|keyboardHidden" />
Kindly help me which is the problem & what should I do. Any help would be appreciated.
Add to your activity manifest
android:clearTaskOnLaunch="true"
According to your Manifest .Main is the launcher activity. so when your application is launched from icon tap then this is the first activity to display and rather if your application is not having any other activity on top of the stack.
so if you want to see the fresh login activity then make the login activity as launcher activity.
Maybe you want to try setting launchMode to standard?
http://developer.android.com/guide/topics/manifest/activity-element.html

Categories

Resources