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.
Related
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.
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 m developing an android application.When I wish to close it,I need to close all the activities sequentially that I have opened earlier.Is there any code which closes the previously opened activity automatically when a new one is opened ?? And where it needs to be written ??
You can use finish() method before or after you intent to another activity.Like this
Intent toAnotherActivity = new Intent(this, YourActivity.class);
startActivity(toAnotherActivity);
finish();
In your AndroidManifest.xml file, just add android:noHistory="true" attribute for all your activities. for example
<activity
android:name=".ExampleActivity"
android:noHistory="true" />
Quote from http://developer.android.com/guide/topics/manifest/activity-element.html
android:noHistoryWhether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen — "true" if it should be finished, and "false" if not. The default value is "false".
A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.
This attribute was introduced in API Level 3.
Possible duplicate:
How to not show app on recent apps list in ICS? excludeFromRecents doesn't work
Let's say I have two activities A (the main activity) and B. I start B from A as follows:
Intent i = new Intent(A.this, B.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
I want my app to have just one entry in the recents at any time AND B to be started at a new task. If B has a manifest entry as follows, I achieve that:
<activity
android:name="com.example.verysimpleactivity2.app.B"
android:excludeFromRecents="true"
android:label="B" ></activity>
But I also want B to have a different taskAffinity than the main activity, i.e. A. So I change the manifest entry into:
<activity
android:name="com.example.verysimpleactivity2.app.B"
android:excludeFromRecents="true"
android:taskAffinity=""
android:label="B" ></activity>
In this case, if B is not in the foreground then it does not show up in the recents and I only have A (good!) However, if B is in the foreground and I press recents button, I see both A and B there. Why is B shown in the recents in this case? How to make sure I don't see B at all?
Apparently, it is impossible to achieve what I want (at least in KitKat).
Source: an Android engineer
A bit late but also adding the answer from androidManifest activity android:excludeFromRecents works. Just in case anyone else was having the same issue and though it couldn't work.
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.