下面列出了android.content.Intent#FLAG_ACTIVITY_NEW_DOCUMENT 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private int adjustLaunchFlagsToDocumentMode(ActivityRecord r, boolean launchSingleInstance,
boolean launchSingleTask, int launchFlags) {
if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
(launchSingleInstance || launchSingleTask)) {
// We have a conflict between the Intent and the Activity manifest, manifest wins.
Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
"\"singleInstance\" or \"singleTask\"");
launchFlags &=
~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_MULTIPLE_TASK);
} else {
switch (r.info.documentLaunchMode) {
case ActivityInfo.DOCUMENT_LAUNCH_NONE:
break;
case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
break;
case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
break;
case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
launchFlags &= ~FLAG_ACTIVITY_MULTIPLE_TASK;
break;
}
}
return launchFlags;
}
@OnClick(R.id.about_link_rate)
public void onRate() {
Uri uri = Uri.parse(String.format("market://details?id=%s", BuildConfig.APPLICATION_ID));
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
/* To count with Play market backstack, After pressing back button,
* to taken back to our application, we need to add following flags to intent. */
int flags = Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
flags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
goToMarket.addFlags(flags);
try {
startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(String.format("http://play.google.com/store/apps/details?id=%s", BuildConfig.APPLICATION_ID))));
}
}
private boolean restartInNewTask() {
boolean hasNewTask = (getIntent().getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) != 0;
boolean hasNewDocument = (getIntent().getFlags() & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
if (hasNewTask && !hasNewDocument) return false;
// The desired behaviour of TWAs is that there's only one running in the user's Recent
// Apps. This reflects how many other Android Apps work and corresponds to ensuring that
// the TWA only exists in a single Task.
// If the TWA was implemented as single Activity, we could do this with
// launchMode=singleTask (or singleInstance), however since the TWA consists of a
// LauncherActivity which then starts a browser Activity, things get a bit more
// complicated.
// If we used singleTask on LauncherActivity then whenever a TWA was running and a new
// Intent was fired, the browser Activity on top would get clobbered.
// Therefore, we always ensure that LauncherActivity is launched with New Task. This
// means that if the TWA is already running a *new* LauncherActivity will be created on
// top of the Browser Activity. The browser then launches an Intent with CLEAR_TOP to
// the existing Browser Activity, killing the temporary LauncherActivity and focusing
// the TWA.
// We also need to clear NEW_DOCUMENT here as well otherwise Intents created with
// NEW_DOCUMENT will launch us in a new Task, separate from an existing instance.
// Setting documentLaunchMode="never" didn't stop this behaviour.
Intent newIntent = new Intent(getIntent());
int flags = getIntent().getFlags();
flags |= Intent.FLAG_ACTIVITY_NEW_TASK;
flags &= ~Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
newIntent.setFlags(flags);
startActivity(newIntent);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_share: {
MovieDetails movieDetails = mViewModel.getResult().getValue().data;
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setSubject(movieDetails.getMovie().getTitle() + " movie trailer")
.setText("Check out " + movieDetails.getMovie().getTitle() + " movie trailer at " +
Uri.parse(Constants.YOUTUBE_WEB_URL +
movieDetails.getTrailers().get(0).getKey())
)
.createChooserIntent();
int flags = Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
if (Build.VERSION.SDK_INT >= 21)
flags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
shareIntent.addFlags(flags);
if (shareIntent.resolveActivity(getPackageManager()) != null) {
startActivity(shareIntent);
}
return true;
}
case R.id.action_favorite: {
mViewModel.onFavoriteClicked();
invalidateOptionsMenu();
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
@SuppressWarnings("deprecation")
public static int getActivityNewDocumentFlag() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
} else {
return Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET;
}
}
/**
* Opens a new Tab with the possibility of showing it in a Custom Tab, instead.
*
* See IntentHandler#processUrlViewIntent() for an explanation most of the parameters.
* @param forceNewTab If not handled by a Custom Tab, forces the new tab to be created.
*/
private void openNewTab(String url, String referer, String headers,
String externalAppId, Intent intent, boolean forceNewTab) {
boolean isAllowedToReturnToExternalApp = IntentUtils.safeGetBooleanExtra(intent,
ChromeLauncherActivity.EXTRA_IS_ALLOWED_TO_RETURN_TO_PARENT, true);
String herbFlavor = FeatureUtilities.getHerbFlavor();
if (isAllowedToReturnToExternalApp
&& ChromeLauncherActivity.canBeHijackedByHerb(intent)
&& TextUtils.equals(ChromeSwitches.HERB_FLAVOR_DILL, herbFlavor)) {
Log.d(TAG, "Sending to CustomTabActivity");
mActivityStopMetrics.setStopReason(
ActivityStopMetrics.STOP_REASON_CUSTOM_TAB_STARTED);
Intent newIntent = ChromeLauncherActivity.createCustomTabActivityIntent(
ChromeTabbedActivity.this, intent, false);
newIntent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName());
newIntent.putExtra(
CustomTabIntentDataProvider.EXTRA_IS_OPENED_BY_CHROME, true);
ChromeLauncherActivity.updateHerbIntent(ChromeTabbedActivity.this,
newIntent, Uri.parse(IntentHandler.getUrlFromIntent(newIntent)));
// Launch the Activity on top of this task.
int updatedFlags = newIntent.getFlags();
updatedFlags &= ~Intent.FLAG_ACTIVITY_NEW_TASK;
updatedFlags &= ~Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
newIntent.setFlags(updatedFlags);
startActivityForResult(newIntent, CCT_RESULT);
} else {
// Create a new tab.
Tab newTab =
launchIntent(url, referer, headers, externalAppId, forceNewTab, intent);
newTab.setIsAllowedToReturnToExternalApp(isAllowedToReturnToExternalApp);
RecordUserAction.record("MobileReceivedExternalIntent");
}
}
/**
* Records metrics gleaned from the Intent.
*/
private void recordIntentMetrics() {
Intent intent = getIntent();
IntentHandler.ExternalAppId source =
IntentHandler.determineExternalIntentSource(getPackageName(), intent);
if (intent.getPackage() == null && source != IntentHandler.ExternalAppId.CHROME) {
int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
int maskedFlags = intent.getFlags() & flagsOfInterest;
sIntentFlagsHistogram.record(maskedFlags);
}
MediaNotificationUma.recordClickSource(intent);
}
/**
* Creates an Intent that can be used to launch a {@link CustomTabActivity}.
*/
public static Intent createCustomTabActivityIntent(
Context context, Intent intent, boolean addHerbExtras) {
// Use the copy constructor to carry over the myriad of extras.
Uri uri = Uri.parse(IntentHandler.getUrlFromIntent(intent));
Intent newIntent = new Intent(intent);
newIntent.setAction(Intent.ACTION_VIEW);
newIntent.setClassName(context, CustomTabActivity.class.getName());
newIntent.setData(uri);
// If a CCT intent triggers First Run, then NEW_TASK will be automatically applied. As
// part of that, it will inherit the EXCLUDE_FROM_RECENTS bit from ChromeLauncherActivity,
// so explicitly remove it to ensure the CCT does not get lost in recents.
if ((newIntent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
|| (newIntent.getFlags() & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0) {
newIntent.setFlags(
newIntent.getFlags() & ~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
}
if (addHerbExtras) {
// TODO(tedchoc|mariakhomenko): Specifically not marking the intent is from Chrome via
// IntentHandler.addTrustedIntentExtras as it breaks the
// redirect logic for triggering instant apps. See if
// this is better addressed in TabRedirectHandler long
// term.
newIntent.putExtra(CustomTabIntentDataProvider.EXTRA_IS_OPENED_BY_CHROME, true);
} else {
IntentUtils.safeRemoveExtra(
intent, CustomTabIntentDataProvider.EXTRA_IS_OPENED_BY_CHROME);
}
if (addHerbExtras) updateHerbIntent(context, newIntent);
return newIntent;
}
/**
* Records metrics gleaned from the Intent.
*/
private void recordIntentMetrics() {
Intent intent = getIntent();
IntentHandler.ExternalAppId source =
IntentHandler.determineExternalIntentSource(getPackageName(), intent);
if (intent.getPackage() == null && source != IntentHandler.ExternalAppId.CHROME) {
int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
int maskedFlags = intent.getFlags() & flagsOfInterest;
sIntentFlagsHistogram.record(maskedFlags);
}
MediaNotificationUma.recordClickSource(intent);
}
@SuppressWarnings("deprecation")
public static int getActivityNewDocumentFlag() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
} else {
return Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET;
}
}
@Override
@SuppressFBWarnings("URF_UNREAD_FIELD")
protected boolean isStartedUpCorrectly(Intent intent) {
if (intent == null
|| !BrowserActionsIntent.ACTION_BROWSER_ACTIONS_OPEN.equals(intent.getAction())) {
return false;
}
mUri = Uri.parse(IntentHandler.getUrlFromIntent(intent));
mType = IntentUtils.safeGetIntExtra(
intent, BrowserActionsIntent.EXTRA_TYPE, BrowserActionsIntent.URL_TYPE_NONE);
mCreatorPackageName = BrowserActionsIntent.getCreatorPackageName(intent);
ArrayList<Bundle> bundles = IntentUtils.getParcelableArrayListExtra(
intent, BrowserActionsIntent.EXTRA_MENU_ITEMS);
if (bundles != null) {
parseBrowserActionItems(bundles);
}
if (mUri == null) {
Log.e(TAG, "Missing url");
return false;
} else if (!UrlConstants.HTTP_SCHEME.equals(mUri.getScheme())
&& !UrlConstants.HTTPS_SCHEME.equals(mUri.getScheme())) {
Log.e(TAG, "Url should only be HTTP or HTTPS scheme");
return false;
} else if (mCreatorPackageName == null) {
Log.e(TAG, "Missing creator's pacakge name");
return false;
} else if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Log.e(TAG, "Intent should not be started with FLAG_ACTIVITY_NEW_TASK");
return false;
} else if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0) {
Log.e(TAG, "Intent should not be started with FLAG_ACTIVITY_NEW_DOCUMENT");
return false;
} else {
return true;
}
}
/**
* Records metrics gleaned from the Intent.
*/
private void recordIntentMetrics() {
Intent intent = getIntent();
IntentHandler.ExternalAppId source =
IntentHandler.determineExternalIntentSource(getPackageName(), intent);
if (intent.getPackage() == null && source != IntentHandler.ExternalAppId.CHROME) {
int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
int maskedFlags = intent.getFlags() & flagsOfInterest;
sIntentFlagsHistogram.record(maskedFlags);
}
MediaNotificationUma.recordClickSource(intent);
}
static boolean isDocumentLaunchesIntoExisting(int flags) {
return (flags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
(flags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0;
}