I have an Activity with launch mode singleTop which shows permission dialog.
But when permission dialog is present and a new Intent comes for that activity , another instance is created .
onNewIntent() doesn't get called.
Any workaround for this?
Below is the manifest entry.
<activity
android:name=".auth.activity.AJRAuthActivity"
android:screenOrientation="portrait"
android:theme="#style/Theme.PageIndicatorDefaults"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustResize" />
The way I start this activity:
Intent loginIntent = new Intent(context, AJRAuthActivity.class);
context.startActivity(loginIntent);
Showing the permission dialog usually starts another Activity (a system Activity) on top of your Activity. In this case, your Activity will be paused (onPause() is called). At that point, your Activity is no longer the top Activity on the stack, so that another launch of the Activity will create another instance, even if "singleTop" launch mode and/or Intent.FLAG_ACTIVITY_SINGLE_TOP is specified.
See Request permission dialog pauses my activity
To get around this, you should ensure that you have all the permissions that you might need as early as possible, or rearchitect your application so that you don't have the problem of multiple instances.
Related
I have a problem with an activity which is started with FLAG_ACTIVITY_REORDER_TO_FRONT is not recreated and present on the screen after FLAG_ACTIVITY_CLEAR_TASK.
So the step is:
Open app, enter Welcome Activity.
Finish Welcome activity and start activity A without specific intent flag.
Start activity B with FLAG_ACTIVITY_REORDER_TO_FRONT from activity A (new instance of B is created and now stack became A->B).
Start activity A from B with FLAG_ACTIVITY_REORDER_TO_FRONT (existing A instance brought to top of stack so stack became B->A).
And under some condition, I need to start over from the beginning (just like another normal app launch), so started Welcome activity using FLAG_ACTIVITY_CLEAR_TASK.
So the app will enter phase 2 again after phase 1, which is what I expected, but then, if I try to start activity B again with FLAG_ACTIVITY_REORDER_TO_FRONT from activity A, there is activity B's callback 'onNewIntent, onStart, onResume' in a row, but it doesn't present on the screen.
It looks like to me that there is still the previous instance of activity B somewhere but not showing to me.
I don't specify launch mode for either activity A or B.
I know that document says about FLAG_ACTIVITY_CLEAR_TASK that "This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.". And use them together does solve my problem, but then if I click home button to put app background and then launch again, it will become another app launch (enter phase 1) but not back to the previous top activity.
So my questions are:
When I use FLAG_ACTIVITY_CLEAR_TASK to start welcome activity, I don't see onDestroy of either activity A or B, is it by design?
If they are not destroyed, where do they stay and can I have a reference to them?
How can I make activity B presented on the screen after all of these steps?
Code for phase 5:
Intent i = new Intent(A.this, WelcomeActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
Thanks in advance.
AndroidManifest.xml
<activity
android:name=".activity.WelcomeActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
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=".activity.A_Activity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait"
android:theme="#style/BaiduMapTheme.MainMap"
android:windowSoftInputMode="adjustPan" />
<activity
android:name=".activity.B_Activity"
android:configChanges="locale|fontScale|keyboard|keyboardHidden|layoutDirection|mcc|mnc|navigation|orientation|screenLayout|screenSize|touchscreen|uiMode"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateVisible" />
OK, now I think I have those questions because of my incorrect using of the intent flags.
First of all, I should use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK at the same time as the document says.
Then everything looks normal and as expected, except one that: if I put the app into background and then click the launch icon again, the WelcomeActivity will be created again.
Previously I thought my existing activity stack is cleared and a new WelcomeActivity is created, but it turns out the existing activity stack is still there, but an extra WelcomeActivity is created, so what I need to do is add some extra flag in WelcomeActivity to determine if it's created in this condition, if yes, then just call finish() in onCreate(), then I can back to the previous activity I was in before enter background.
I have LoginActivity where after successful logging I start MainActivity via intent and finish LoginActivity.
I press back button and then open app via icon and It shows me MainActivity but if I open app from recent apps list after pressing back button so I see LoginActivity
I've checked if LoginActivity was destroyed
How can It be?
manifest
<activity android:name=".LoginActivity" />
<activity android:name=".MainActivity"
android:launchMode="singleTask" />
start MainActivity
Intent intent = new Intent(getActivity(), MainActivity.class);
mProgressDialog.dismiss();
startActivity(intent);
getActivity().finish();
Remove android:launchMode="singleTask"
Why you are adding launchMode, adding this you will be able to get its instance only once. Let default be "Standard", for more information, please have a look at the documentation.
Docs say:
The "standard" and "singleTop" modes differ from each other in just one respect: Every time there's a new intent for a "standard" activity, a new instance of the class is created to respond to that intent. Each instance handles a single intent. Similarly, a new instance of a "singleTop" activity may also be created to handle a new intent. However, 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.
So, you wouldn't need singleTask launch mode. Apart from this, I can't see <intent-filter> for your LoginActivity as being MAIN action and LAUNCHER category.
For an App I am developing, I override the back button to make it act like the home button so that the state of the main activity is preserved even when the app is exited. Now, I also send a notification to the user from time to time using a service. When this notification is pressed I want to open the main activity again. I noticed though that this creates a second instance of the app, which creates major problems. I am trying to make the main activity go to the front again, without calling oncreate again like so:
Intent to launch main activity again:
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
This doesn't work though. I still end up with two instances of my main activity. Does anybody know how to fix this?
By the way, I already have android:launchMode="singleInstance" in my manifest.
There's a way to force the OS to create only one instance of an activity and thats using the tag launchMode in the Manifest as shown below:
<activity android:name="YourActivity"
android:launchMode="singleInstance"/>
Hope this Helps...
Regards
Try adding this flag to the intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP), works for me.
I have activity A where authentication is done. After this user redirected to other activities say B,C,D. Suppose now the user is at Activity C and suddenly closes the application, next time when he launches the application it is displaying Activity C (the last activity run by user before exiting App) but,But i want to start Activity A (Authentication Purpose) when user relaunches application irrespective of where he closes the application.
Help Needed
You can remove an Activity from Stack by adding android:noHistory="true" attribute in AndroidManifest.xml
<activity android:name="CurrentActivity"
android:launchMode="singleInstance"
android:noHistory="true" />
or by code:
Intent intent = new Intent(CurrentActivity_A.this,CurrentActivity_C.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.startActivity(intent);
Use android:noHistory="true" attribute to your activity tag in the manifest file. It makes sure that your Activity doesn't stay in the stack when it goes to background. Here's a full description. Hope this helps.
I know there's been a few posts for what I'm about to ask but I can't find any with the right answer.
From my understanding, if your main activity's (let's call it A) launchMode is set to singleTask, and A has initiated activity B then a click to the home button will destroy the history stack and re-launching the application will take you back to A and not B.
I have launchMode set to singleTask because I have a persistent notification and I don't want to have multiple instances of the main activity to appear whenever the user clicks on the notification.
Is there something I'm missing that would allow me to cater for both?
So I'm asking if there's a way I can ensure that whenever the user wishes to launch the app, from the notification or not, to take him back to the last (current) activity.
If I change launchMode to singleTop it works but I get multiple instances of the main activity whenever I launch it.
Thanks
Andreas
Have you tried setting launchMode to singleTop to all the activities in your app?? Because what i get from your query is that the main activity isn't singleTop, so that might lead to another instance of the main activity being called once the main activity is launched from the activity that was launched from the notification activity.
Or you can specify the launchMode as an attribute to the application tag itself in the manifest.
I use the following code to avoid multiple instances of the activity
Intent intent=new Intent(this,RICO.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
Changing manifest doesn't look appropriate to me
I'm having issues with both the approches.
The notification works flawless only in this condition:
- using the back button in the main activity (with the history containing only the that activity)
- not using the Home button
- not using the notification IF the activity you are calling is on top and active
In any other case, the notification cannot anymore call on the foreground the activity by using "new Intent(...)"
I've found the alchemical combination of manifest options and intent's flags for getting what I needed:
Intent intent= new Intent(this, YaampActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
using these options
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"
inside the element.
Now I've a notification which spawns the main activity (if that activity is not already in the foreground) and its behavior is correct even if the activity is "closed" by pressing the home button and/or the back one.