You are on page 1of 29

package com.blankj.utilcode.

util;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.AnimRes;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.util.Pair;
import android.view.View;

import java.util.List;

/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2016/09/23
* desc : utils about activity
* </pre>
*/
public final class ActivityUtils {

private ActivityUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}

/**
* Return whether the activity exists.
*
* @param pkg The name of the package.
* @param cls The name of the class.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isActivityExists(@NonNull final String pkg,
@NonNull final String cls) {
Intent intent = new Intent();
intent.setClassName(pkg, cls);
return !(Utils.getApp().getPackageManager().resolveActivity(intent, 0) ==
null ||
intent.resolveActivity(Utils.getApp().getPackageManager()) == null
||
Utils.getApp().getPackageManager().queryIntentActivities(intent,
0).size() == 0);
}

/**
* Start the activity.
*
* @param clz The activity class.
*/
public static void startActivity(@NonNull final Class<? extends Activity> clz)
{
Context context = Utils.getTopActivityOrApp();
startActivity(context, null, context.getPackageName(), clz.getName(),
null);
}

/**
* Start the activity.
*
* @param clz The activity class.
* @param options Additional options for how the Activity should be started.
*/
public static void startActivity(@NonNull final Class<? extends Activity> clz,
final Bundle options) {
Context context = Utils.getTopActivityOrApp();
startActivity(context, null, context.getPackageName(), clz.getName(),
options);
}

/**
* Start the activity.
*
* @param clz The activity class.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void startActivity(@NonNull final Class<? extends Activity> clz,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
Context context = Utils.getTopActivityOrApp();
startActivity(context, null, context.getPackageName(), clz.getName(),
getOptionsBundle(context, enterAnim, exitAnim));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN && context
instanceof Activity) {
((Activity) context).overridePendingTransition(enterAnim, exitAnim);
}
}

/**
* Start the activity.
*
* @param activity The activity.
* @param clz The activity class.
*/
public static void startActivity(@NonNull final Activity activity,
@NonNull final Class<? extends Activity> clz)
{
startActivity(activity, null, activity.getPackageName(), clz.getName(),
null);
}

/**
* Start the activity.
*
* @param activity The activity.
* @param clz The activity class.
* @param options Additional options for how the Activity should be started.
*/
public static void startActivity(@NonNull final Activity activity,
@NonNull final Class<? extends Activity> clz,
final Bundle options) {
startActivity(activity, null, activity.getPackageName(), clz.getName(),
options);
}

/**
* Start the activity.
*
* @param activity The activity.
* @param clz The activity class.
* @param sharedElements The names of the shared elements to transfer to the
called
* Activity and their associated Views.
*/
public static void startActivity(@NonNull final Activity activity,
@NonNull final Class<? extends Activity> clz,
final View... sharedElements) {
startActivity(activity, null, activity.getPackageName(), clz.getName(),
getOptionsBundle(activity, sharedElements));
}

/**
* Start the activity.
*
* @param activity The activity.
* @param clz The activity class.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void startActivity(@NonNull final Activity activity,
@NonNull final Class<? extends Activity> clz,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
startActivity(activity, null, activity.getPackageName(), clz.getName(),
getOptionsBundle(activity, enterAnim, exitAnim));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
activity.overridePendingTransition(enterAnim, exitAnim);
}
}

/**
* Start the activity.
*
* @param extras The Bundle of extras to add to this intent.
* @param clz The activity class.
*/
public static void startActivity(@NonNull final Bundle extras,
@NonNull final Class<? extends Activity> clz)
{
Context context = Utils.getTopActivityOrApp();
startActivity(context, extras, context.getPackageName(), clz.getName(),
null);
}

/**
* Start the activity.
*
* @param extras The Bundle of extras to add to this intent.
* @param clz The activity class.
* @param options Additional options for how the Activity should be started.
*/
public static void startActivity(@NonNull final Bundle extras,
@NonNull final Class<? extends Activity> clz,
@NonNull final Bundle options) {
Context context = Utils.getTopActivityOrApp();
startActivity(context, extras, context.getPackageName(), clz.getName(),
options);
}

/**
* Start the activity.
*
* @param extras The Bundle of extras to add to this intent.
* @param clz The activity class.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void startActivity(@NonNull final Bundle extras,
@NonNull final Class<? extends Activity> clz,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
Context context = Utils.getTopActivityOrApp();
startActivity(context, extras, context.getPackageName(), clz.getName(),
getOptionsBundle(context, enterAnim, exitAnim));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN && context
instanceof Activity) {
((Activity) context).overridePendingTransition(enterAnim, exitAnim);
}
}

/**
* Start the activity.
*
* @param extras The Bundle of extras to add to this intent.
* @param activity The activity.
* @param clz The activity class.
*/
public static void startActivity(@NonNull final Bundle extras,
@NonNull final Activity activity,
@NonNull final Class<? extends Activity> clz)
{
startActivity(activity, extras, activity.getPackageName(), clz.getName(),
null);
}

/**
* Start the activity.
*
* @param extras The Bundle of extras to add to this intent.
* @param activity The activity.
* @param clz The activity class.
* @param options Additional options for how the Activity should be started.
*/
public static void startActivity(@NonNull final Bundle extras,
@NonNull final Activity activity,
@NonNull final Class<? extends Activity> clz,
@NonNull final Bundle options) {
startActivity(activity, extras, activity.getPackageName(), clz.getName(),
options);
}

/**
* Start the activity.
*
* @param extras The Bundle of extras to add to this intent.
* @param activity The activity.
* @param clz The activity class.
* @param sharedElements The names of the shared elements to transfer to the
called
* Activity and their associated Views.
*/
public static void startActivity(@NonNull final Bundle extras,
@NonNull final Activity activity,
@NonNull final Class<? extends Activity> clz,
final View... sharedElements) {
startActivity(activity, extras, activity.getPackageName(), clz.getName(),
getOptionsBundle(activity, sharedElements));
}

/**
* Start the activity.
*
* @param extras The Bundle of extras to add to this intent.
* @param activity The activity.
* @param clz The activity class.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void startActivity(@NonNull final Bundle extras,
@NonNull final Activity activity,
@NonNull final Class<? extends Activity> clz,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
startActivity(activity, extras, activity.getPackageName(), clz.getName(),
getOptionsBundle(activity, enterAnim, exitAnim));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
activity.overridePendingTransition(enterAnim, exitAnim);
}
}

/**
* Start the activity.
*
* @param pkg The name of the package.
* @param cls The name of the class.
*/
public static void startActivity(@NonNull final String pkg,
@NonNull final String cls) {
startActivity(Utils.getTopActivityOrApp(), null, pkg, cls, null);
}

/**
* Start the activity.
*
* @param pkg The name of the package.
* @param cls The name of the class.
* @param options Additional options for how the Activity should be started.
*/
public static void startActivity(@NonNull final String pkg,
@NonNull final String cls,
final Bundle options) {
startActivity(Utils.getTopActivityOrApp(), null, pkg, cls, options);
}

/**
* Start the activity.
*
* @param pkg The name of the package.
* @param cls The name of the class.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void startActivity(@NonNull final String pkg,
@NonNull final String cls,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
Context context = Utils.getTopActivityOrApp();
startActivity(context, null, pkg, cls, getOptionsBundle(context, enterAnim,
exitAnim));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN && context
instanceof Activity) {
((Activity) context).overridePendingTransition(enterAnim, exitAnim);
}
}

/**
* Start the activity.
*
* @param activity The activity.
* @param pkg The name of the package.
* @param cls The name of the class.
*/
public static void startActivity(@NonNull final Activity activity,
@NonNull final String pkg,
@NonNull final String cls) {
startActivity(activity, null, pkg, cls, null);
}

/**
* Start the activity.
*
* @param activity The activity.
* @param pkg The name of the package.
* @param cls The name of the class.
* @param options Additional options for how the Activity should be started.
*/
public static void startActivity(@NonNull final Activity activity,
@NonNull final String pkg,
@NonNull final String cls,
final Bundle options) {
startActivity(activity, null, pkg, cls, options);
}

/**
* Start the activity.
*
* @param activity The activity.
* @param pkg The name of the package.
* @param cls The name of the class.
* @param sharedElements The names of the shared elements to transfer to the
called
* Activity and their associated Views.
*/
public static void startActivity(@NonNull final Activity activity,
@NonNull final String pkg,
@NonNull final String cls,
final View... sharedElements) {
startActivity(activity, null, pkg, cls, getOptionsBundle(activity,
sharedElements));
}

/**
* Start the activity.
*
* @param activity The activity.
* @param pkg The name of the package.
* @param cls The name of the class.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void startActivity(@NonNull final Activity activity,
@NonNull final String pkg,
@NonNull final String cls,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
startActivity(activity, null, pkg, cls, getOptionsBundle(activity,
enterAnim, exitAnim));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
activity.overridePendingTransition(enterAnim, exitAnim);
}
}

/**
* Start the activity.
*
* @param extras The Bundle of extras to add to this intent.
* @param pkg The name of the package.
* @param cls The name of the class.
*/
public static void startActivity(@NonNull final Bundle extras,
@NonNull final String pkg,
@NonNull final String cls) {
startActivity(Utils.getTopActivityOrApp(), extras, pkg, cls, null);
}

/**
* Start the activity.
*
* @param extras The Bundle of extras to add to this intent.
* @param pkg The name of the package.
* @param cls The name of the class.
* @param options Additional options for how the Activity should be started.
*/
public static void startActivity(@NonNull final Bundle extras,
@NonNull final String pkg,
@NonNull final String cls,
final Bundle options) {
startActivity(Utils.getTopActivityOrApp(), extras, pkg, cls, options);
}

/**
* Start the activity.
*
* @param extras The Bundle of extras to add to this intent.
* @param pkg The name of the package.
* @param cls The name of the class.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void startActivity(@NonNull final Bundle extras,
@NonNull final String pkg,
@NonNull final String cls,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
Context context = Utils.getTopActivityOrApp();
startActivity(context, extras, pkg, cls, getOptionsBundle(context,
enterAnim, exitAnim));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN && context
instanceof Activity) {
((Activity) context).overridePendingTransition(enterAnim, exitAnim);
}
}

/**
* Start the activity.
*
* @param activity The activity.
* @param extras The Bundle of extras to add to this intent.
* @param pkg The name of the package.
* @param cls The name of the class.
*/
public static void startActivity(@NonNull final Bundle extras,
@NonNull final Activity activity,
@NonNull final String pkg,
@NonNull final String cls) {
startActivity(activity, extras, pkg, cls, null);
}

/**
* Start the activity.
*
* @param extras The Bundle of extras to add to this intent.
* @param activity The activity.
* @param pkg The name of the package.
* @param cls The name of the class.
* @param options Additional options for how the Activity should be started.
*/
public static void startActivity(@NonNull final Bundle extras,
@NonNull final Activity activity,
@NonNull final String pkg,
@NonNull final String cls,
final Bundle options) {
startActivity(activity, extras, pkg, cls, options);
}

/**
* Start the activity.
*
* @param extras The Bundle of extras to add to this intent.
* @param activity The activity.
* @param pkg The name of the package.
* @param cls The name of the class.
* @param sharedElements The names of the shared elements to transfer to the
called
* Activity and their associated Views.
*/
public static void startActivity(@NonNull final Bundle extras,
@NonNull final Activity activity,
@NonNull final String pkg,
@NonNull final String cls,
final View... sharedElements) {
startActivity(activity, extras, pkg, cls, getOptionsBundle(activity,
sharedElements));
}

/**
* Start the activity.
*
* @param extras The Bundle of extras to add to this intent.
* @param pkg The name of the package.
* @param cls The name of the class.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void startActivity(@NonNull final Bundle extras,
@NonNull final Activity activity,
@NonNull final String pkg,
@NonNull final String cls,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
startActivity(activity, extras, pkg, cls, getOptionsBundle(activity,
enterAnim, exitAnim));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
activity.overridePendingTransition(enterAnim, exitAnim);
}
}
/**
* Start the activity.
*
* @param intent The description of the activity to start.
*/
public static void startActivity(@NonNull final Intent intent) {
startActivity(intent, Utils.getTopActivityOrApp(), null);
}

/**
* Start the activity.
*
* @param intent The description of the activity to start.
* @param options Additional options for how the Activity should be started.
*/
public static void startActivity(@NonNull final Intent intent,
@NonNull final Bundle options) {
startActivity(intent, Utils.getTopActivityOrApp(), options);
}

/**
* Start the activity.
*
* @param intent The description of the activity to start.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void startActivity(@NonNull final Intent intent,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
Context context = Utils.getTopActivityOrApp();
startActivity(intent, context, getOptionsBundle(context, enterAnim,
exitAnim));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN && context
instanceof Activity) {
((Activity) context).overridePendingTransition(enterAnim, exitAnim);
}
}

/**
* Start the activity.
*
* @param activity The activity.
* @param intent The description of the activity to start.
*/
public static void startActivity(@NonNull final Activity activity,
@NonNull final Intent intent) {
startActivity(intent, activity, null);
}

/**
* Start the activity.
*
* @param activity The activity.
* @param intent The description of the activity to start.
* @param options Additional options for how the Activity should be started.
*/
public static void startActivity(@NonNull final Activity activity,
@NonNull final Intent intent,
final Bundle options) {
startActivity(intent, activity, options);
}

/**
* Start the activity.
*
* @param activity The activity.
* @param intent The description of the activity to start.
* @param sharedElements The names of the shared elements to transfer to the
called
* Activity and their associated Views.
*/
public static void startActivity(@NonNull final Activity activity,
@NonNull final Intent intent,
final View... sharedElements) {
startActivity(intent, activity, getOptionsBundle(activity,
sharedElements));
}

/**
* Start the activity.
*
* @param activity The activity.
* @param intent The description of the activity to start.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void startActivity(@NonNull final Activity activity,
@NonNull final Intent intent,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
startActivity(intent, activity, getOptionsBundle(activity, enterAnim,
exitAnim));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
activity.overridePendingTransition(enterAnim, exitAnim);
}
}

/**
* Start the activity.
*
* @param activity The activity.
* @param clz The activity class.
* @param requestCode if &gt;= 0, this code will be returned in
* onActivityResult() when the activity exits.
*/
public static void startActivityForResult(@NonNull final Activity activity,
@NonNull final Class<? extends
Activity> clz,
final int requestCode) {
startActivityForResult(activity, null, activity.getPackageName(),
clz.getName(),
requestCode, null);
}
/**
* Start the activity.
*
* @param activity The activity.
* @param clz The activity class.
* @param requestCode if &gt;= 0, this code will be returned in
* onActivityResult() when the activity exits.
* @param options Additional options for how the Activity should be
started.
*/
public static void startActivityForResult(@NonNull final Activity activity,
@NonNull final Class<? extends
Activity> clz,
final int requestCode,
final Bundle options) {
startActivityForResult(activity, null, activity.getPackageName(),
clz.getName(),
requestCode, options);
}

/**
* Start the activity.
*
* @param activity The activity.
* @param clz The activity class.
* @param requestCode if &gt;= 0, this code will be returned in
* onActivityResult() when the activity exits.
* @param sharedElements The names of the shared elements to transfer to the
called
* Activity and their associated Views.
*/
public static void startActivityForResult(@NonNull final Activity activity,
@NonNull final Class<? extends
Activity> clz,
final int requestCode,
final View... sharedElements) {
startActivityForResult(activity, null, activity.getPackageName(),
clz.getName(),
requestCode, getOptionsBundle(activity, sharedElements));
}

/**
* Start the activity.
*
* @param activity The activity.
* @param clz The activity class.
* @param requestCode if &gt;= 0, this code will be returned in
* onActivityResult() when the activity exits.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void startActivityForResult(@NonNull final Activity activity,
@NonNull final Class<? extends
Activity> clz,
final int requestCode,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
startActivityForResult(activity, null, activity.getPackageName(),
clz.getName(),
requestCode, getOptionsBundle(activity, enterAnim, exitAnim));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
activity.overridePendingTransition(enterAnim, exitAnim);
}
}

/**
* Start the activity.
*
* @param extras The Bundle of extras to add to this intent.
* @param activity The activity.
* @param clz The activity class.
* @param requestCode if &gt;= 0, this code will be returned in
* onActivityResult() when the activity exits.
*/
public static void startActivityForResult(@NonNull final Bundle extras,
@NonNull final Activity activity,
@NonNull final Class<? extends
Activity> clz,
final int requestCode) {
startActivityForResult(activity, extras, activity.getPackageName(),
clz.getName(),
requestCode, null);
}

/**
* Start the activity.
*
* @param extras The Bundle of extras to add to this intent.
* @param activity The activity.
* @param clz The activity class.
* @param requestCode if &gt;= 0, this code will be returned in
* onActivityResult() when the activity exits.
* @param options Additional options for how the Activity should be
started.
*/
public static void startActivityForResult(@NonNull final Bundle extras,
@NonNull final Activity activity,
@NonNull final Class<? extends
Activity> clz,
final int requestCode,
@NonNull final Bundle options) {
startActivityForResult(activity, extras, activity.getPackageName(),
clz.getName(),
requestCode, options);
}

/**
* Start the activity.
*
* @param extras The Bundle of extras to add to this intent.
* @param activity The activity.
* @param clz The activity class.
* @param requestCode if &gt;= 0, this code will be returned in
* onActivityResult() when the activity exits.
* @param sharedElements The names of the shared elements to transfer to the
called
* Activity and their associated Views.
*/
public static void startActivityForResult(@NonNull final Bundle extras,
@NonNull final Activity activity,
@NonNull final Class<? extends
Activity> clz,
final int requestCode,
final View... sharedElements) {
startActivityForResult(activity, extras, activity.getPackageName(),
clz.getName(),
requestCode, getOptionsBundle(activity, sharedElements));
}

/**
* Start the activity.
*
* @param extras The Bundle of extras to add to this intent.
* @param activity The activity.
* @param clz The activity class.
* @param requestCode if &gt;= 0, this code will be returned in
* onActivityResult() when the activity exits.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void startActivityForResult(@NonNull final Bundle extras,
@NonNull final Activity activity,
@NonNull final Class<? extends
Activity> clz,
final int requestCode,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
startActivityForResult(activity, extras, activity.getPackageName(),
clz.getName(),
requestCode, getOptionsBundle(activity, enterAnim, exitAnim));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
activity.overridePendingTransition(enterAnim, exitAnim);
}
}

/**
* Start the activity for result.
*
* @param activity The activity.
* @param extras The Bundle of extras to add to this intent.
* @param pkg The name of the package.
* @param cls The name of the class.
* @param requestCode if &gt;= 0, this code will be returned in
* onActivityResult() when the activity exits.
*/
public static void startActivityForResult(@NonNull final Bundle extras,
@NonNull final Activity activity,
@NonNull final String pkg,
@NonNull final String cls,
final int requestCode) {
startActivityForResult(activity, extras, pkg, cls, requestCode, null);
}
/**
* Start the activity for result.
*
* @param extras The Bundle of extras to add to this intent.
* @param activity The activity.
* @param pkg The name of the package.
* @param cls The name of the class.
* @param requestCode if &gt;= 0, this code will be returned in
* onActivityResult() when the activity exits.
* @param options Additional options for how the Activity should be
started.
*/
public static void startActivityForResult(@NonNull final Bundle extras,
@NonNull final Activity activity,
@NonNull final String pkg,
@NonNull final String cls,
final int requestCode,
final Bundle options) {
startActivityForResult(activity, extras, pkg, cls, requestCode, options);
}

/**
* Start the activity for result.
*
* @param extras The Bundle of extras to add to this intent.
* @param activity The activity.
* @param pkg The name of the package.
* @param cls The name of the class.
* @param requestCode if &gt;= 0, this code will be returned in
* onActivityResult() when the activity exits.
* @param sharedElements The names of the shared elements to transfer to the
called
* Activity and their associated Views.
*/
public static void startActivityForResult(@NonNull final Bundle extras,
@NonNull final Activity activity,
@NonNull final String pkg,
@NonNull final String cls,
final int requestCode,
final View... sharedElements) {
startActivityForResult(activity, extras, pkg, cls,
requestCode, getOptionsBundle(activity, sharedElements));
}

/**
* Start the activity for result.
*
* @param extras The Bundle of extras to add to this intent.
* @param pkg The name of the package.
* @param cls The name of the class.
* @param requestCode if &gt;= 0, this code will be returned in
* onActivityResult() when the activity exits.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void startActivityForResult(@NonNull final Bundle extras,
@NonNull final Activity activity,
@NonNull final String pkg,
@NonNull final String cls,
final int requestCode,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
startActivityForResult(activity, extras, pkg, cls,
requestCode, getOptionsBundle(activity, enterAnim, exitAnim));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
activity.overridePendingTransition(enterAnim, exitAnim);
}
}

/**
* Start the activity for result.
*
* @param activity The activity.
* @param intent The description of the activity to start.
* @param requestCode if &gt;= 0, this code will be returned in
* onActivityResult() when the activity exits.
*/
public static void startActivityForResult(@NonNull final Activity activity,
@NonNull final Intent intent,
final int requestCode) {
startActivityForResult(intent, activity, requestCode, null);
}

/**
* Start the activity for result.
*
* @param activity The activity.
* @param intent The description of the activity to start.
* @param requestCode if &gt;= 0, this code will be returned in
* onActivityResult() when the activity exits.
* @param options Additional options for how the Activity should be
started.
*/
public static void startActivityForResult(@NonNull final Activity activity,
@NonNull final Intent intent,
final int requestCode,
final Bundle options) {
startActivityForResult(intent, activity, requestCode, options);
}

/**
* Start the activity for result.
*
* @param activity The activity.
* @param intent The description of the activity to start.
* @param requestCode if &gt;= 0, this code will be returned in
* onActivityResult() when the activity exits.
* @param sharedElements The names of the shared elements to transfer to the
called
* Activity and their associated Views.
*/
public static void startActivityForResult(@NonNull final Activity activity,
@NonNull final Intent intent,
final int requestCode,
final View... sharedElements) {
startActivityForResult(intent, activity,
requestCode, getOptionsBundle(activity, sharedElements));
}

/**
* Start the activity for result.
*
* @param activity The activity.
* @param intent The description of the activity to start.
* @param requestCode if &gt;= 0, this code will be returned in
* onActivityResult() when the activity exits.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void startActivityForResult(@NonNull final Activity activity,
@NonNull final Intent intent,
final int requestCode,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
startActivityForResult(intent, activity,
requestCode, getOptionsBundle(activity, enterAnim, exitAnim));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
activity.overridePendingTransition(enterAnim, exitAnim);
}
}

/**
* Start activities.
*
* @param intents The descriptions of the activities to start.
*/
public static void startActivities(@NonNull final Intent[] intents) {
startActivities(intents, Utils.getTopActivityOrApp(), null);
}

/**
* Start activities.
*
* @param intents The descriptions of the activities to start.
* @param options Additional options for how the Activity should be started.
*/
public static void startActivities(@NonNull final Intent[] intents,
final Bundle options) {
startActivities(intents, Utils.getTopActivityOrApp(), options);
}

/**
* Start activities.
*
* @param intents The descriptions of the activities to start.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void startActivities(@NonNull final Intent[] intents,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
Context context = Utils.getTopActivityOrApp();
startActivities(intents, context, getOptionsBundle(context, enterAnim,
exitAnim));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN && context
instanceof Activity) {
((Activity) context).overridePendingTransition(enterAnim, exitAnim);
}
}

/**
* Start activities.
*
* @param activity The activity.
* @param intents The descriptions of the activities to start.
*/
public static void startActivities(@NonNull final Activity activity,
@NonNull final Intent[] intents) {
startActivities(intents, activity, null);
}

/**
* Start activities.
*
* @param activity The activity.
* @param intents The descriptions of the activities to start.
* @param options Additional options for how the Activity should be started.
*/
public static void startActivities(@NonNull final Activity activity,
@NonNull final Intent[] intents,
final Bundle options) {
startActivities(intents, activity, options);
}

/**
* Start activities.
*
* @param activity The activity.
* @param intents The descriptions of the activities to start.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void startActivities(@NonNull final Activity activity,
@NonNull final Intent[] intents,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
startActivities(intents, activity, getOptionsBundle(activity, enterAnim,
exitAnim));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
activity.overridePendingTransition(enterAnim, exitAnim);
}
}

/**
* Start home activity.
*/
public static void startHomeActivity() {
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
startActivity(homeIntent);
}

/**
* Return the list of activity.
*
* @return the list of activity
*/
public static List<Activity> getActivityList() {
return Utils.getActivityList();
}

/**
* Return the name of launcher activity.
*
* @return the name of launcher activity
*/
public static String getLauncherActivity() {
return getLauncherActivity(Utils.getApp().getPackageName());
}

/**
* Return the name of launcher activity.
*
* @param pkg The name of the package.
* @return the name of launcher activity
*/
public static String getLauncherActivity(@NonNull final String pkg) {
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PackageManager pm = Utils.getApp().getPackageManager();
List<ResolveInfo> info = pm.queryIntentActivities(intent, 0);
for (ResolveInfo aInfo : info) {
if (aInfo.activityInfo.packageName.equals(pkg)) {
return aInfo.activityInfo.name;
}
}
return "no " + pkg;
}

/**
* Return the top activity in activity's stack.
*
* @return the top activity in activity's stack
*/
public static Activity getTopActivity() {
return Utils.getActivityLifecycle().getTopActivity();
}

/**
* Return whether the activity exists in activity's stack.
*
* @param activity The activity.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isActivityExistsInStack(@NonNull final Activity activity)
{
List<Activity> activities = Utils.getActivityList();
for (Activity aActivity : activities) {
if (aActivity.equals(activity)) {
return true;
}
}
return false;
}

/**
* Return whether the activity exists in activity's stack.
*
* @param clz The activity class.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isActivityExistsInStack(@NonNull final Class<? extends
Activity> clz) {
List<Activity> activities = Utils.getActivityList();
for (Activity aActivity : activities) {
if (aActivity.getClass().equals(clz)) {
return true;
}
}
return false;
}

/**
* Finish the activity.
*
* @param activity The activity.
*/
public static void finishActivity(@NonNull final Activity activity) {
finishActivity(activity, false);
}

/**
* Finish the activity.
*
* @param activity The activity.
* @param isLoadAnim True to use animation for the outgoing activity, false
otherwise.
*/
public static void finishActivity(@NonNull final Activity activity, final
boolean isLoadAnim) {
activity.finish();
if (!isLoadAnim) {
activity.overridePendingTransition(0, 0);
}
}

/**
* Finish the activity.
*
* @param activity The activity.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void finishActivity(@NonNull final Activity activity,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
activity.finish();
activity.overridePendingTransition(enterAnim, exitAnim);
}

/**
* Finish the activity.
*
* @param clz The activity class.
*/
public static void finishActivity(@NonNull final Class<? extends Activity> clz)
{
finishActivity(clz, false);
}

/**
* Finish the activity.
*
* @param clz The activity class.
* @param isLoadAnim True to use animation for the outgoing activity, false
otherwise.
*/
public static void finishActivity(@NonNull final Class<? extends Activity> clz,
final boolean isLoadAnim) {
List<Activity> activities = Utils.getActivityList();
for (Activity activity : activities) {
if (activity.getClass().equals(clz)) {
activity.finish();
if (!isLoadAnim) {
activity.overridePendingTransition(0, 0);
}
}
}
}

/**
* Finish the activity.
*
* @param clz The activity class.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void finishActivity(@NonNull final Class<? extends Activity> clz,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
List<Activity> activities = Utils.getActivityList();
for (Activity activity : activities) {
if (activity.getClass().equals(clz)) {
activity.finish();
activity.overridePendingTransition(enterAnim, exitAnim);
}
}
}
/**
* Finish to the activity.
*
* @param activity The activity.
* @param isIncludeSelf True to include the activity, false otherwise.
*/
public static boolean finishToActivity(@NonNull final Activity activity,
final boolean isIncludeSelf) {
return finishToActivity(activity, isIncludeSelf, false);
}

/**
* Finish to the activity.
*
* @param activity The activity.
* @param isIncludeSelf True to include the activity, false otherwise.
* @param isLoadAnim True to use animation for the outgoing activity, false
otherwise.
*/
public static boolean finishToActivity(@NonNull final Activity activity,
final boolean isIncludeSelf,
final boolean isLoadAnim) {
List<Activity> activities = Utils.getActivityList();
for (int i = activities.size() - 1; i >= 0; --i) {
Activity aActivity = activities.get(i);
if (aActivity.equals(activity)) {
if (isIncludeSelf) {
finishActivity(aActivity, isLoadAnim);
}
return true;
}
finishActivity(aActivity, isLoadAnim);
}
return false;
}

/**
* Finish to the activity.
*
* @param activity The activity.
* @param isIncludeSelf True to include the activity, false otherwise.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static boolean finishToActivity(@NonNull final Activity activity,
final boolean isIncludeSelf,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
List<Activity> activities = Utils.getActivityList();
for (int i = activities.size() - 1; i >= 0; --i) {
Activity aActivity = activities.get(i);
if (aActivity.equals(activity)) {
if (isIncludeSelf) {
finishActivity(aActivity, enterAnim, exitAnim);
}
return true;
}
finishActivity(aActivity, enterAnim, exitAnim);
}
return false;
}

/**
* Finish to the activity.
*
* @param clz The activity class.
* @param isIncludeSelf True to include the activity, false otherwise.
*/
public static boolean finishToActivity(@NonNull final Class<? extends Activity>
clz,
final boolean isIncludeSelf) {
return finishToActivity(clz, isIncludeSelf, false);
}

/**
* Finish to the activity.
*
* @param clz The activity class.
* @param isIncludeSelf True to include the activity, false otherwise.
* @param isLoadAnim True to use animation for the outgoing activity, false
otherwise.
*/
public static boolean finishToActivity(@NonNull final Class<? extends Activity>
clz,
final boolean isIncludeSelf,
final boolean isLoadAnim) {
List<Activity> activities = Utils.getActivityList();
for (int i = activities.size() - 1; i >= 0; --i) {
Activity aActivity = activities.get(i);
if (aActivity.getClass().equals(clz)) {
if (isIncludeSelf) {
finishActivity(aActivity, isLoadAnim);
}
return true;
}
finishActivity(aActivity, isLoadAnim);
}
return false;
}

/**
* Finish to the activity.
*
* @param clz The activity class.
* @param isIncludeSelf True to include the activity, false otherwise.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static boolean finishToActivity(@NonNull final Class<? extends Activity>
clz,
final boolean isIncludeSelf,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
List<Activity> activities = Utils.getActivityList();
for (int i = activities.size() - 1; i >= 0; --i) {
Activity aActivity = activities.get(i);
if (aActivity.getClass().equals(clz)) {
if (isIncludeSelf) {
finishActivity(aActivity, enterAnim, exitAnim);
}
return true;
}
finishActivity(aActivity, enterAnim, exitAnim);
}
return false;
}

/**
* Finish the activities whose type not equals the activity class.
*
* @param clz The activity class.
*/
public static void finishOtherActivities(@NonNull final Class<? extends
Activity> clz) {
finishOtherActivities(clz, false);
}

/**
* Finish the activities whose type not equals the activity class.
*
* @param clz The activity class.
* @param isLoadAnim True to use animation for the outgoing activity, false
otherwise.
*/
public static void finishOtherActivities(@NonNull final Class<? extends
Activity> clz,
final boolean isLoadAnim) {
List<Activity> activities = Utils.getActivityList();
for (int i = activities.size() - 1; i >= 0; i--) {
Activity activity = activities.get(i);
if (!activity.getClass().equals(clz)) {
finishActivity(activity, isLoadAnim);
}
}
}

/**
* Finish the activities whose type not equals the activity class.
*
* @param clz The activity class.
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void finishOtherActivities(@NonNull final Class<? extends
Activity> clz,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
List<Activity> activities = Utils.getActivityList();
for (int i = activities.size() - 1; i >= 0; i--) {
Activity activity = activities.get(i);
if (!activity.getClass().equals(clz)) {
finishActivity(activity, enterAnim, exitAnim);
}
}
}

/**
* Finish all of activities.
*/
public static void finishAllActivities() {
finishAllActivities(false);
}

/**
* Finish all of activities.
*
* @param isLoadAnim True to use animation for the outgoing activity, false
otherwise.
*/
public static void finishAllActivities(final boolean isLoadAnim) {
List<Activity> activityList = Utils.getActivityList();
for (int i = activityList.size() - 1; i >= 0; --i) {// remove from top
Activity activity = activityList.get(i);
// sActivityList remove the index activity at onActivityDestroyed
activity.finish();
if (!isLoadAnim) {
activity.overridePendingTransition(0, 0);
}
}
}

/**
* Finish all of activities.
*
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void finishAllActivities(@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
List<Activity> activityList = Utils.getActivityList();
for (int i = activityList.size() - 1; i >= 0; --i) {// remove from top
Activity activity = activityList.get(i);
// sActivityList remove the index activity at onActivityDestroyed
activity.finish();
activity.overridePendingTransition(enterAnim, exitAnim);
}
}

/**
* Finish all of activities except the newest activity.
*/
public static void finishAllActivitiesExceptNewest() {
finishAllActivitiesExceptNewest(false);
}

/**
* Finish all of activities except the newest activity.
*
* @param isLoadAnim True to use animation for the outgoing activity, false
otherwise.
*/
public static void finishAllActivitiesExceptNewest(final boolean isLoadAnim) {
List<Activity> activities = Utils.getActivityList();
for (int i = activities.size() - 2; i >= 0; i--) {
finishActivity(activities.get(i), isLoadAnim);
}
}

/**
* Finish all of activities except the newest activity.
*
* @param enterAnim A resource ID of the animation resource to use for the
* incoming activity.
* @param exitAnim A resource ID of the animation resource to use for the
* outgoing activity.
*/
public static void finishAllActivitiesExceptNewest(@AnimRes final int
enterAnim,
@AnimRes final int exitAnim)
{
List<Activity> activities = Utils.getActivityList();
for (int i = activities.size() - 2; i >= 0; i--) {
finishActivity(activities.get(i), enterAnim, exitAnim);
}
}

/**
* Return the icon of activity.
*
* @param activity The activity.
* @return the icon of activity
*/
public static Drawable getActivityIcon(@NonNull final Activity activity) {
return getActivityIcon(activity.getComponentName());
}

/**
* Return the icon of activity.
*
* @param clz The activity class.
* @return the icon of activity
*/
public static Drawable getActivityIcon(@NonNull final Class<? extends Activity>
clz) {
return getActivityIcon(new ComponentName(Utils.getApp(), clz));
}

/**
* Return the icon of activity.
*
* @param activityName The name of activity.
* @return the icon of activity
*/
public static Drawable getActivityIcon(@NonNull final ComponentName
activityName) {
PackageManager pm = Utils.getApp().getPackageManager();
try {
return pm.getActivityIcon(activityName);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return null;
}
}

/**
* Return the logo of activity.
*
* @param activity The activity.
* @return the logo of activity
*/
public static Drawable getActivityLogo(@NonNull final Activity activity) {
return getActivityLogo(activity.getComponentName());
}

/**
* Return the logo of activity.
*
* @param clz The activity class.
* @return the logo of activity
*/
public static Drawable getActivityLogo(@NonNull final Class<? extends Activity>
clz) {
return getActivityLogo(new ComponentName(Utils.getApp(), clz));
}

/**
* Return the logo of activity.
*
* @param activityName The name of activity.
* @return the logo of activity
*/
public static Drawable getActivityLogo(@NonNull final ComponentName
activityName) {
PackageManager pm = Utils.getApp().getPackageManager();
try {
return pm.getActivityLogo(activityName);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return null;
}
}

private static void startActivity(final Context context,


final Bundle extras,
final String pkg,
final String cls,
final Bundle options) {
Intent intent = new Intent(Intent.ACTION_VIEW);
if (extras != null) intent.putExtras(extras);
intent.setComponent(new ComponentName(pkg, cls));
startActivity(intent, context, options);
}

private static void startActivity(final Intent intent,


final Context context,
final Bundle options) {
if (!(context instanceof Activity)) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
if (options != null && Build.VERSION.SDK_INT >=
Build.VERSION_CODES.JELLY_BEAN) {
context.startActivity(intent, options);
} else {
context.startActivity(intent);
}
}

private static void startActivityForResult(final Activity activity,


final Bundle extras,
final String pkg,
final String cls,
final int requestCode,
final Bundle options) {
Intent intent = new Intent(Intent.ACTION_VIEW);
if (extras != null) intent.putExtras(extras);
intent.setComponent(new ComponentName(pkg, cls));
startActivityForResult(intent, activity, requestCode, options);
}

private static void startActivityForResult(final Intent intent,


final Activity activity,
final int requestCode,
final Bundle options) {
if (options != null && Build.VERSION.SDK_INT >=
Build.VERSION_CODES.JELLY_BEAN) {
activity.startActivityForResult(intent, requestCode, options);
} else {
activity.startActivityForResult(intent, requestCode);
}
}

private static void startActivities(final Intent[] intents,


final Context context,
final Bundle options) {
if (!(context instanceof Activity)) {
for (Intent intent : intents) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
}
if (options != null && Build.VERSION.SDK_INT >=
Build.VERSION_CODES.JELLY_BEAN) {
context.startActivities(intents, options);
} else {
context.startActivities(intents);
}
}

private static Bundle getOptionsBundle(final Context context,


final int enterAnim,
final int exitAnim) {
return ActivityOptionsCompat.makeCustomAnimation(context, enterAnim,
exitAnim).toBundle();
}
private static Bundle getOptionsBundle(final Activity activity,
final View[] sharedElements) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return null;
if (sharedElements == null) return null;
int len = sharedElements.length;
if (len <= 0) return null;
@SuppressWarnings("unchecked")
Pair<View, String>[] pairs = new Pair[len];
for (int i = 0; i < len; i++) {
pairs[i] = Pair.create(sharedElements[i],
sharedElements[i].getTransitionName());
}
return ActivityOptionsCompat.makeSceneTransitionAnimation(activity,
pairs).toBundle();
}
}

You might also like