startActivity does not create activity the second time - android

I have two applications installed on my device, application A and application B.
When a user launches application A, they press a button to launch application B.
I use the following code:
Intent intent = Global.CurrentContext.getPackageManager().getLaunchIntentForPackage("some.random.app");
Global.CurrentContext.startActivity(intent);
The first time the user does this, it works fine and works as expected, however, if the user then launches the phones task manager and swipes away application B. When the user then presses the button in application A. The activity is not created.
I believe this is something to do with the way that the task and the activity are being destroyed by android. My thought process is that when the user swipes to kill application B that the activity is destroyed, but the task remains. Then the second time they press the button in application A, that it's attaching to the old task and then not showing for the user.
Application A's activity manifest for the activity:
<activity
android:name="a.a.a.LogonActivity"
android:label="#string/app_name"
android:noHistory="true"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Light.NoTitleBar"
android:windowSoftInputMode="stateHidden"/>
Application B's activity manifest for the activity:
<activity
android:name=".activity.HomeActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

When I set android:noHistory="false", it works. I think it is android:noHistory="true" killed applicationA's top activity, makes it doesn't work.

Related

Launcher development - Home button not going back to initial activity

I have an app that acts as a Launcher. This app has 3 activities:
SplashActivity: shows a splash screen while loading, then launches LauncherActivity and finishes. This is the Activity marked as launcher in the manifest.
startActivity(Intent(this, LauncherActivity::class.java))
finish()
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
LauncherActivity: main activity for Launcher. Has a menu button that launches DashboardActivity.
startActivity(Intent(this#LauncherActivity, DashboardActivity::class.java))
<activity
android:name=".LauncherActivity"
android:launchMode="singleTask"
android:screenOrientation="landscape" />
DashboardActivity: shows a list of apps and launches them through their launch intent.
private val DEFAULT_FLAGS_APP_LAUNCH = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(packageManager.getLaunchIntentForPackage(packageInfo.packageName).apply {
flags = DEFAULT_FLAGS_APP_LAUNCH
})
<activity
android:label="#string/apps"
android:theme="#style/TNA"
android:name=".DashboardActivity"
android:launchMode="singleTask"
android:screenOrientation="landscape" />
All activities are launched through startActivity, including the apps.
I want the standard Android Launcher behavior, that is: when entering an app through DashboardActivity, if I click home button, go to the main Launcher activity (LauncherActivity), and when clicking back, go to the dashboard (DashboardActivity).
The problem I have is that when clicking home, it goes back to DashboardActivity, not to LauncherActivity. If I finish DashboardActivity, then when clicking back on an app, it goes back to LauncherActivity.
Any ideas on how to solve this?
This is definitely back/task stack related. See this link for more information about the task stack.
When you go from LauncherActivity to DashboardActivity, the dashboard is placed on to the task stack. When the LauncherActivity is requested again via the HOME button, the task stack is restored back to the last Activity which was in use after launching the LauncherActivity, which was DashboardActivity.
You have several different options to resolve this:
Don't use a separate Activity for the "dashboard". Consider a drawer or even a Fragment which shows the content and can be popped back to the main LauncherActivity when it is done calling startActivity to launch another app.
After your DashboardActivity calls startActivity, it should call finish() so it will get popped off the current task stack.
Usually launchers are setup to be launched in singleInstance mode, preventing multiple instances of the launcher Activity to run at the same time. Note that you'll need to support onNewIntent in your LauncherActivity.
To prevent odd interactions with the task manager, consider setting FLAG_ACTIVITY_NO_HISTORY when launcher your DashboardActivity.

launch a app splash screen everytime we click a app icon from home screen

What is happening:
Currently when i click the app icon for the first time i am loading
SplashActivity and then loading ActAtomicGodDetailDesc
Now when i minimize the app -- > then go to home screen -- > then
click on the app icon again ActAtomicGodDetailDesc is displaying
instead of SplashActivity
What i want:
Everytime i want the app to fire SplashActivity when i click the app icon from the home screen
manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:noHistory="true"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ActAtomicGodDetailDesc"
android:label="#string/app_name"
android:screenOrientation="portrait" >
</activity>
</application>
How to achieve this ? any ideas
Everytime i want the app to fire SplashActivity when i click the app icon from the home screen
I think you require to set android:clearTaskOnLaunch="true" on Splash Activity such that whenever you click on app icon then it will start as root activity.
android:clearTaskOnLaunch http://developer.android.com/guide/topics/manifest/activity-element.html#clear
Whether or not all activities will be removed from the task, except for the root activity, whenever it is re-launched from the home screen — "true" if the task is always stripped down to its root activity, and "false" if not. The default value is "false". This attribute is meaningful only for activities that start a new task (the root activity); it's ignored for all other activities in the task.
When the value is "true", every time users start the task again, they are brought to its root activity regardless of what they were last doing in the task and regardless of whether they used the Back or Home button to leave it. When the value is "false", the task may be cleared of activities in some situations (see the alwaysRetainTaskState attribute), but not always.
Suppose, for example, that someone launches activity P from the home screen, and from there goes to activity Q. The user next presses Home, and then returns to activity P. Normally, the user would see activity Q, since that is what they were last doing in P's task. However, if P set this flag to "true", all of the activities on top of it (Q in this case) were removed when the user pressed Home and the task went to the background. So the user sees only P when returning to the task.
If this attribute and allowTaskReparenting are both "true", any activities that can be re-parented are moved to the task they share an affinity with; the remaining activities are then dropped, as described above.
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:clearTaskOnLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

How open not main activity from Notification

In my app I have a Activity that is opened from a Notification, but is not declared has main activity or launcher activity·
Also, this activity is declared has "singleTop".
Works fine for most users, but some are a little problem.
In some occassions when click the Notification the Activity is opened and, when click back, the app Main Activity is shown.
How to don't show the main activity when click back?
Also, if the users are in this activity and click home, and later it clicks into the app icon in order to open the Main activity, the previously opened activity, that is not main activity is opened. This can change?
Thanks.
-- The structure of the manifest is:
<activity android:name=".MainActivity" android:label="#string/app_name" android:exported="true" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ShowAlarmsActivity" android:launchMode="singleTop" android:exported="true" android:theme="#style/AppThemeWithoutActionBar" />
you may do this my either overriding on back press and starting the parent activity using this code
or if you are using action bar you may use parent activity tag
Intent intent = new Intent(getApplicationContext(), ParentActivity.class);
startActivity(intent);
also if you want to distinguish that if the user is coming from notification or from app launch you may use intent to pass some key value and cater it
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="parentActivity" />

Root Activity is getting called again and again when coming from background to foreground instead of last activity in stack

I am developing an hybrid app using IBM worklight where I have to open another webview on the top of existing webview(Cordova webview),so i created a new activity(WebView Activity) to load my external page,but when i am going to background from webview activity by pressing home button and again coming to foreground my MainActivity getting called and restarts the app,I have used singleTop for my MainActivity in manifest file
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden|screenLayout|screenSize"
android:launchMode="singleTop"
>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="com.main.sample.NOTIFICATION"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".WebViewActivity"
android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden|screenLayout|screenSize"
android:launchMode="singleTop"
android:theme="#style/Theme.Transparent">
</activity>
I don't know why it is happening,I want my last activity to get resumed instead of main activity
Read documentation for the modes.This will explain you about what is happening:
"singleTask" and "singleInstance", should be used only when the > activity has an ACTION_MAIN and a CATEGORY_LAUNCHER filter. > Imagine, for example, what could happen if the filter is missing: An > intent launches a "singleTask" activity, initiating a new task, and > the user spends some time working in that task. The user then presses > the Home button. The task is now sent to the background and is not > visible. Now the user has no way to return to the task, because it is > not represented in the application launcher.
This is one potential solution.There are multiple ways to do this.
Try using the following code in the onCreate method of the activity that is specified as the Launcher Activity in the Manifest:
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
finish();
return;
}
This will finish your Launcher Activity before it is displayed by detecting that there is already a task running, and your app should instead resume to the last visible Activity.

Android Activity Task in foreground

I have 3 activity tasks A-B-C
<activity android:name=".LoginActivity"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".DashboardActivity"
android:label="#string/app_name" >
</activity>
<activity android:name=".CreateNewPolygonActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:alwaysRetainTaskState="true"
android:configChanges="orientation|keyboard|keyboardHidden">
</activity>
<service android:name=".services.RecordCircuitService"
android:enabled="true" >
</service>
And I have a Service with GPS using LocationManager.
My application has this logic:
On launcher icon clicked - > start login activity
After login - > login activity is finish(); and start dashboardActivity
In the dashboard I start CreateNewPolygonActivity and start the Service with GPS
Press HOME (this is reorganize RecordActivity to foreground)
after I have some mistakes work
open CreateNewPolygonActivity with onCreate
open DashboardActivity
I think I have a problem with my task organization.
You won't achieve the desired result by setting launchMode="singleTask" for your activity. Android won't create a separate task for this because you've not set the taskAffinity. In any case, you don't want to do this by using launchMode="singleTask" because this causes more problems than it solves. This launch mode is intended for HOME-screen replacements only!
Remove launchMode="singleTask"`` fromCreateNewPolygonActivity`
Also, you should remove android:noHistory="true" from your login activity. This isn't necessary either. Since you've already finished the login activity when the user starts the DashboardActivity, when he presses the BACK button, it won't return to the login activity (which I assume is what you wanted).
Once you've done these things, please tell us what is still broken.

Categories

Resources