I am trying to familiarize myself with intent flags, and I want to understand FLAG_ACTIVITY_RETAIN_IN_RECENTS. I have read the documentation and it seems like if this flag is set when calling an Activity, if the user presses the back button or that Activity is finished(), it still remains on the stack. Is this interpretation accurate? If not, what is this flag used for?
Reference: http://developer.android.com/reference/android/content/Intent.html
Thank you in advance!
I have read the documentation and it seems like if this flag is set
when calling an Activity, if the user presses the back button or that
Activity is finished(), it still remains on the stack.
No, the Activity will not remain on the stack, but its entry will be shown in the recent task list, you can click on that entry to re-launch this Activity just as you re-launch your application.
Another Usage in OverviewScreen:
If you want to retain a task in the overview screen, even if its activity has finished, pass the FLAG_ACTIVITY_RETAIN_IN_RECENTS flag in the addFlags() method of the Intent that launches the activity.
private Intent newDocumentIntent() {
final Intent newDocumentIntent = new Intent(this, NewDocumentActivity.class);
newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
android.content.Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS);
newDocumentIntent.putExtra(KEY_EXTRA_NEW_DOCUMENT_COUNTER, incrementAndGet());
return newDocumentIntent;
}
To achieve the same effect, set the attribute android:autoRemoveFromRecents to false. The default value is true for document activities, and false for regular activities. Using this attribute overrides the FLAG_ACTIVITY_RETAIN_IN_RECENTS flag
Related
In my application there are 4 activities.
1-LoginOrSignupActivity(Main)
2-SignupActivity
3-LoginActivity
4-MainFeedActivity
Problem is that when i am logged in and i am in MainFeedActivity if i press back but it takes me back to loginOrSignupActivity. I want my application to go on onPause State when back button is pressed within MainFeedActivity.
Flow is like LoginOrSignupActivity->LoginActivity->MainFeedActivity
other flow is like LoginOrSignupActivity->SignupActivity->MainFeedActivity
i used this Flag but still it is not working
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
If you first go to LoginOrSignup, then LoginActivity, and then open MainActivity, you need to call finish() on your passed activities when you move on. Or else the activity will remain in the back stack. Alternatively you can set a flag on your activity in the manifest.
Relevant reading: http://developer.android.com/guide/components/tasks-and-back-stack.html
For those cases where you don't want the user to be able to return to an activity, set the element's finishOnTaskLaunch to "true" (see Clearing the stack).
I have 3 activities in my app.
Home -> List -> Detail
When I am in Detail activity, I want to have a home button that will redirect me to Home activity and close the other activities. Then, I tried this code:
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
since the FLAG_ACTIVITY_CLEAR_TOP is:
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
I got what I want, but then it still goes to onCreate. Is there any way to launch the home and not going to its onCreate?
Your thinking and Android's are not yet fully compatible. ;)
When you "launch the home", that means it's going to be launched, and that means it's going to be created.
You can, however, use Activity.startActivityForResult() to start List and Detail. Your "home" button then will finish your Detail Activity. In the onActivityResult callback of List, you'll just pass through using Activity.finish(), and in onActivityResult of Home you'll just catch the event and know that you're now back.
You cannot control the Activity life cycle. It's up to Android to decide which activities to keep running and which to close. This means that when an Activity loses focus, it is not necessarily destroyed or closed, but might simply lost focus and keep running, but might also not. This is up to Android; you cannot control this behavior.
If you want to recover an Activity in a given state, you have to save all needed data and restore it in onCreate().
I have set setDisplayHomeAsUpEnabled to true and when home is pressed I want the user to go back to the very first Activity.
But in case FirstActivity already is created I don't want to recreate it. I'm currently adding FLAG_ACTIVITY_CLEAR_TOP to the intent that starts FirstActivity. Are there other flags I need to add (or use another flag altogether)
to get the desired behaviour of only creating the Activity if it doesn't exist or is the flag I have sufficient?
you can use singleTop launchmode and override onNewIntent method (this will be called if activity is relaunched instead of new one )
you can't assume that root activity is still alive, In Android 4.0 and greater devices have the developer option called don't keep activities, if it's enabled when you leave from one activity to another the parent activity automatically killed by the system.
In my Android application I have an activity where I've been using [Activity(NoHistory=true)] to keep the activity from appearing on the activity stack.
Now I'd like to make it conditional - sometimes it should be on the activity stack so the user can press Back to return to it from a subsequent screen; sometimes it shouldn't. Is there a way to "decorate" an activity conditionally or must I write some code to accomplish this?
Thanks in advance.
Are you looking for intent flags that can be set like the one below ? You could use that to start an activity and set the flag conditionally.
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
My application starts with a welcome screen Activity, but that screen has an option to skip that screen altogether in future launches.
What's the proper Android way to do this? Initially, I just automatically detected the skipWelcome preference and switched to the 2nd activity from Welcome. But this had the effect of allowing the user to hit the back button to the welcome screen we promised never to show again.
Right now, in the Welcome activity, I read the preference and call finish() on the current activity:
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean skipWelcome = preferences.getBoolean("skipWelcome", false);
if (skipWelcome) {
this.finish();
}
And then I implement onDestroy to move on to the next Activity:
#Override
public void onDestroy() {
super.onDestroy();
startActivity(new Intent(Welcome.this, StartFoo.class));
}
But this makes for some weird visual transitions. I'm starting to think that I need a base Activity that pops open Welcome only if proper, and then goes to StartFoo.
I can't comment on Mayra's answer or I would (not enough rep), but that's the correct approach.
Hidden in the Android documentation is this important phrase for Activity.startActivityForResult(),
"As a special case, if you call
startActivityForResult() with a
requestCode >= 0 during the initial
onCreate(Bundle
savedInstanceState)/onResume() of your
activity, then your window will not be
displayed until a result is returned
back from the started activity. This
is to avoid visible flickering when
redirecting to another activity."
Another important note is that this call does not block and execution continues, so you need to stop execution of the onCreate by returning
if (skipWelcome) {
// Create intent
// Launch intent with startActivityForResult()
return;
}
The final piece is to call finish immediately in the welcome activity's onActivityResult as Mayra says.
There are a few solutions to this.
Did you try just launching the activity and finishing? I vauguely remember that working, but I could be wrong.
More correctly, in if(skipWelcome) you can start the new activity for result, then when onActivityResult is called, immidiately finish the welcome activity.
Or, you can have your launcher activity not have a view (don't set content), and launch either the welcome activity or StartFoo.