| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- package com.grkj.iscs.util
- import android.R
- import android.app.Activity
- import android.app.ActivityManager
- import android.app.ActivityOptions
- import android.content.Context
- import android.content.Intent
- import android.os.Bundle
- import android.util.Pair
- import android.view.View
- import androidx.core.app.ActivityCompat
- import androidx.core.app.ActivityOptionsCompat
- import java.util.*
- import kotlin.system.exitProcess
- /**
- * @author tamsiree
- * @date 2016/1/24
- *
- *
- * 封装Activity相关工具类
- */
- object ActivityUtils {
- var activityStack: Stack<Activity?>? = null
- /**
- * 获取栈中指定Activity实例
- *
- * @param T activity
- * @return 栈中存在的指定Activity/null
- */
- inline fun <reified T> getSpecActivity(): T? {
- activityStack?.forEach {
- it?.let { itActivity ->
- if (itActivity is T) {
- return itActivity
- }
- }
- }
- return null
- }
- /**
- * 清理指定activity之后的activity
- */
- inline fun <reified T> clearSpecTop() {
- activityStack?.asReversed()?.forEach {
- if (it !is T) {
- it?.finish()
- }
- }
- }
- /**
- * 添加Activity 到栈
- *
- * @param activity Activity
- */
- @JvmStatic
- fun addActivity(activity: Activity?) {
- if (activityStack == null) {
- activityStack = Stack()
- }
- activityStack?.add(activity)
- }
- /**
- * 从List中移除活动
- *
- * @param activity 活动
- */
- @JvmStatic
- fun removeActivity(activity: Activity?) {
- if (activity != null) {
- if (activityStack!!.contains(activity)) {
- activityStack?.remove(activity)
- }
- }
- }
- /**
- * 获取当前的Activity(堆栈中最后一个压入的)
- */
- @JvmStatic
- fun currentActivity(): Activity? {
- return activityStack?.lastElement()
- }
- /**
- * 结束当前Activity(堆栈中最后一个压入的)
- */
- @JvmStatic
- @JvmOverloads
- fun finishActivity(isTransition: Boolean = false) {
- val activity = activityStack?.lastElement()
- if (isTransition) {
- activity?.onBackPressed()
- } else {
- activity?.finish()
- }
- }
- /**
- * 结束指定类名的Activity
- */
- @JvmStatic
- fun finishActivity(cls: Class<out Activity>) {
- for (activity in activityStack!!) {
- if (activity!!.javaClass == cls) {
- finishActivity(activity)
- }
- }
- }
- /**
- * 结束所有的Activity
- */
- @JvmStatic
- fun finishAllActivity() {
- val size = activityStack!!.size
- for (i in 0 until size) {
- if (null != activityStack!![i]) {
- activityStack!![i]!!.finish()
- }
- }
- activityStack!!.clear()
- exitProcess(0)
- }
- /**
- * 判断是否存在指定Activity
- *
- * @param context 上下文
- * @param packageName 包名
- * @param className activity全路径类名
- * @return `true`: 是<br></br>`false`: 否
- */
- @JvmStatic
- fun isExistActivity(context: Context, packageName: String?, className: String?): Boolean {
- val intent = Intent()
- intent.setClassName(packageName!!, className!!)
- return !(context.packageManager.resolveActivity(intent, 0) == null || intent.resolveActivity(context.packageManager) == null || context.packageManager.queryIntentActivities(intent, 0).size == 0)
- }
- /**
- * 要求最低API为11
- * Activity 跳转
- * 跳转后Finish之前所有的Activity
- *
- * @param context Context
- * @param goal Activity
- */
- /**
- * 要求最低API为11
- * Activity 跳转
- * 跳转后Finish之前所有的Activity
- *
- * @param context Context
- * @param goal Activity
- */
- @JvmStatic
- @JvmOverloads
- fun skipActivityAndFinishAll(context: Context, goal: Class<out Activity>?, bundle: Bundle? = null, isFade: Boolean = false) {
- val intent = Intent(context, goal)
- if (bundle != null) {
- intent.putExtras(bundle)
- }
- intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
- context.startActivity(intent)
- finishActivity(context, false)
- if (isFade) {
- fadeTransition(context)
- }
- }
- /**
- * Activity 跳转
- *
- * @param context Context
- * @param goal Activity
- */
- @JvmStatic
- fun skipActivityAndFinish(context: Context, goal: Class<out Activity>?, isFade: Boolean = false, isTransition: Boolean = false) {
- skipActivity(context, goal, null, isFade)
- finishActivity(context, isTransition)
- }
- /**
- * Activity 跳转
- *
- * @param context Context
- * @param goal Activity
- */
- @JvmStatic
- @JvmOverloads
- fun skipActivityAndFinish(context: Context, goal: Class<out Activity>?, bundle: Bundle? = null, isFade: Boolean = false, isTransition: Boolean = false) {
- skipActivity(context, goal, bundle, isFade)
- finishActivity(context, isTransition)
- }
- /**
- * Activity 跳转
- *
- * @param context Context
- * @param goal Activity
- */
- @JvmStatic
- @JvmOverloads
- fun skipActivity(context: Context, goal: Class<out Activity>?, bundle: Bundle? = null, isFade: Boolean = false) {
- val intent = Intent(context, goal)
- if (bundle != null) {
- intent.putExtras(bundle)
- }
- context.startActivity(intent)
- if (isFade) {
- fadeTransition(context)
- }
- }
- @JvmStatic
- @JvmOverloads
- fun skipActivityForResult(context: Activity, goal: Class<out Activity>?, bundle: Bundle? = null, requestCode: Int) {
- val intent = Intent(context, goal)
- if (bundle != null) {
- intent.putExtras(bundle)
- }
- context.startActivityForResult(intent, requestCode)
- }
- @JvmStatic
- @JvmOverloads
- fun skipActivityOnTransitions(mContext: Context?, goal: Class<out Activity>?, bundle: Bundle? = null, vararg pairs: Pair<View, String>?) {
- val intent = Intent(mContext, goal)
- val bundle1 = ActivityOptions.makeSceneTransitionAnimation(mContext as Activity?, *pairs).toBundle()
- if (bundle != null) {
- intent.putExtras(bundle)
- }
- ActivityCompat.startActivity(mContext!!, intent, bundle1)
- }
- @JvmStatic
- @JvmOverloads
- fun skipActivityTransition(mContext: Context, goal: Class<out Activity>?, bundle: Bundle? = null, view: View?, elementName: String?) {
- val intent = Intent(mContext, goal)
- val bundle1 = ActivityOptionsCompat.makeSceneTransitionAnimation((mContext as Activity), view!!, elementName!!).toBundle()
- if (bundle != null) {
- intent.putExtras(bundle)
- }
- mContext.startActivity(intent, bundle1)
- }
- /**
- * 获取launcher activity
- *
- * @param context 上下文
- * @param packageName 包名
- * @return launcher activity
- */
- @JvmStatic
- fun getLauncherActivity(context: Context, packageName: String): String {
- val intent = Intent(Intent.ACTION_MAIN, null)
- intent.addCategory(Intent.CATEGORY_LAUNCHER)
- val pm = context.packageManager
- val infos = pm.queryIntentActivities(intent, 0)
- for (info in infos) {
- if (info.activityInfo.packageName == packageName) {
- return info.activityInfo.name
- }
- }
- return "no $packageName"
- }
- @JvmStatic
- @JvmOverloads
- fun finishActivity(mContext: Context, isTransition: Boolean = false) {
- removeActivity((mContext as Activity))
- if (isTransition) {
- mContext.onBackPressed()
- } else {
- mContext.finish()
- }
- }
- @JvmStatic
- fun fadeTransition(mContext: Context) {
- (mContext as Activity).overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
- }
- }
|