ActivityUtils.kt 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package com.grkj.iscs.util
  2. import android.R
  3. import android.app.Activity
  4. import android.app.ActivityManager
  5. import android.app.ActivityOptions
  6. import android.content.Context
  7. import android.content.Intent
  8. import android.os.Bundle
  9. import android.util.Pair
  10. import android.view.View
  11. import androidx.core.app.ActivityCompat
  12. import androidx.core.app.ActivityOptionsCompat
  13. import java.util.*
  14. import kotlin.system.exitProcess
  15. /**
  16. * @author tamsiree
  17. * @date 2016/1/24
  18. *
  19. *
  20. * 封装Activity相关工具类
  21. */
  22. object ActivityUtils {
  23. var activityStack: Stack<Activity?>? = null
  24. /**
  25. * 获取栈中指定Activity实例
  26. *
  27. * @param T activity
  28. * @return 栈中存在的指定Activity/null
  29. */
  30. inline fun <reified T> getSpecActivity(): T? {
  31. activityStack?.forEach {
  32. it?.let { itActivity ->
  33. if (itActivity is T) {
  34. return itActivity
  35. }
  36. }
  37. }
  38. return null
  39. }
  40. /**
  41. * 清理指定activity之后的activity
  42. */
  43. inline fun <reified T> clearSpecTop() {
  44. activityStack?.asReversed()?.forEach {
  45. if (it !is T) {
  46. it?.finish()
  47. }
  48. }
  49. }
  50. /**
  51. * 添加Activity 到栈
  52. *
  53. * @param activity Activity
  54. */
  55. @JvmStatic
  56. fun addActivity(activity: Activity?) {
  57. if (activityStack == null) {
  58. activityStack = Stack()
  59. }
  60. activityStack?.add(activity)
  61. }
  62. /**
  63. * 从List中移除活动
  64. *
  65. * @param activity 活动
  66. */
  67. @JvmStatic
  68. fun removeActivity(activity: Activity?) {
  69. if (activity != null) {
  70. if (activityStack!!.contains(activity)) {
  71. activityStack?.remove(activity)
  72. }
  73. }
  74. }
  75. /**
  76. * 获取当前的Activity(堆栈中最后一个压入的)
  77. */
  78. @JvmStatic
  79. fun currentActivity(): Activity? {
  80. return activityStack?.lastElement()
  81. }
  82. /**
  83. * 结束当前Activity(堆栈中最后一个压入的)
  84. */
  85. @JvmStatic
  86. @JvmOverloads
  87. fun finishActivity(isTransition: Boolean = false) {
  88. val activity = activityStack?.lastElement()
  89. if (isTransition) {
  90. activity?.onBackPressed()
  91. } else {
  92. activity?.finish()
  93. }
  94. }
  95. /**
  96. * 结束指定类名的Activity
  97. */
  98. @JvmStatic
  99. fun finishActivity(cls: Class<out Activity>) {
  100. for (activity in activityStack!!) {
  101. if (activity!!.javaClass == cls) {
  102. finishActivity(activity)
  103. }
  104. }
  105. }
  106. /**
  107. * 结束所有的Activity
  108. */
  109. @JvmStatic
  110. fun finishAllActivity() {
  111. val size = activityStack!!.size
  112. for (i in 0 until size) {
  113. if (null != activityStack!![i]) {
  114. activityStack!![i]!!.finish()
  115. }
  116. }
  117. activityStack!!.clear()
  118. exitProcess(0)
  119. }
  120. /**
  121. * 判断是否存在指定Activity
  122. *
  123. * @param context 上下文
  124. * @param packageName 包名
  125. * @param className activity全路径类名
  126. * @return `true`: 是<br></br>`false`: 否
  127. */
  128. @JvmStatic
  129. fun isExistActivity(context: Context, packageName: String?, className: String?): Boolean {
  130. val intent = Intent()
  131. intent.setClassName(packageName!!, className!!)
  132. return !(context.packageManager.resolveActivity(intent, 0) == null || intent.resolveActivity(context.packageManager) == null || context.packageManager.queryIntentActivities(intent, 0).size == 0)
  133. }
  134. /**
  135. * 要求最低API为11
  136. * Activity 跳转
  137. * 跳转后Finish之前所有的Activity
  138. *
  139. * @param context Context
  140. * @param goal Activity
  141. */
  142. /**
  143. * 要求最低API为11
  144. * Activity 跳转
  145. * 跳转后Finish之前所有的Activity
  146. *
  147. * @param context Context
  148. * @param goal Activity
  149. */
  150. @JvmStatic
  151. @JvmOverloads
  152. fun skipActivityAndFinishAll(context: Context, goal: Class<out Activity>?, bundle: Bundle? = null, isFade: Boolean = false) {
  153. val intent = Intent(context, goal)
  154. if (bundle != null) {
  155. intent.putExtras(bundle)
  156. }
  157. intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
  158. context.startActivity(intent)
  159. finishActivity(context, false)
  160. if (isFade) {
  161. fadeTransition(context)
  162. }
  163. }
  164. /**
  165. * Activity 跳转
  166. *
  167. * @param context Context
  168. * @param goal Activity
  169. */
  170. @JvmStatic
  171. fun skipActivityAndFinish(context: Context, goal: Class<out Activity>?, isFade: Boolean = false, isTransition: Boolean = false) {
  172. skipActivity(context, goal, null, isFade)
  173. finishActivity(context, isTransition)
  174. }
  175. /**
  176. * Activity 跳转
  177. *
  178. * @param context Context
  179. * @param goal Activity
  180. */
  181. @JvmStatic
  182. @JvmOverloads
  183. fun skipActivityAndFinish(context: Context, goal: Class<out Activity>?, bundle: Bundle? = null, isFade: Boolean = false, isTransition: Boolean = false) {
  184. skipActivity(context, goal, bundle, isFade)
  185. finishActivity(context, isTransition)
  186. }
  187. /**
  188. * Activity 跳转
  189. *
  190. * @param context Context
  191. * @param goal Activity
  192. */
  193. @JvmStatic
  194. @JvmOverloads
  195. fun skipActivity(context: Context, goal: Class<out Activity>?, bundle: Bundle? = null, isFade: Boolean = false) {
  196. val intent = Intent(context, goal)
  197. if (bundle != null) {
  198. intent.putExtras(bundle)
  199. }
  200. context.startActivity(intent)
  201. if (isFade) {
  202. fadeTransition(context)
  203. }
  204. }
  205. @JvmStatic
  206. @JvmOverloads
  207. fun skipActivityForResult(context: Activity, goal: Class<out Activity>?, bundle: Bundle? = null, requestCode: Int) {
  208. val intent = Intent(context, goal)
  209. if (bundle != null) {
  210. intent.putExtras(bundle)
  211. }
  212. context.startActivityForResult(intent, requestCode)
  213. }
  214. @JvmStatic
  215. @JvmOverloads
  216. fun skipActivityOnTransitions(mContext: Context?, goal: Class<out Activity>?, bundle: Bundle? = null, vararg pairs: Pair<View, String>?) {
  217. val intent = Intent(mContext, goal)
  218. val bundle1 = ActivityOptions.makeSceneTransitionAnimation(mContext as Activity?, *pairs).toBundle()
  219. if (bundle != null) {
  220. intent.putExtras(bundle)
  221. }
  222. ActivityCompat.startActivity(mContext!!, intent, bundle1)
  223. }
  224. @JvmStatic
  225. @JvmOverloads
  226. fun skipActivityTransition(mContext: Context, goal: Class<out Activity>?, bundle: Bundle? = null, view: View?, elementName: String?) {
  227. val intent = Intent(mContext, goal)
  228. val bundle1 = ActivityOptionsCompat.makeSceneTransitionAnimation((mContext as Activity), view!!, elementName!!).toBundle()
  229. if (bundle != null) {
  230. intent.putExtras(bundle)
  231. }
  232. mContext.startActivity(intent, bundle1)
  233. }
  234. /**
  235. * 获取launcher activity
  236. *
  237. * @param context 上下文
  238. * @param packageName 包名
  239. * @return launcher activity
  240. */
  241. @JvmStatic
  242. fun getLauncherActivity(context: Context, packageName: String): String {
  243. val intent = Intent(Intent.ACTION_MAIN, null)
  244. intent.addCategory(Intent.CATEGORY_LAUNCHER)
  245. val pm = context.packageManager
  246. val infos = pm.queryIntentActivities(intent, 0)
  247. for (info in infos) {
  248. if (info.activityInfo.packageName == packageName) {
  249. return info.activityInfo.name
  250. }
  251. }
  252. return "no $packageName"
  253. }
  254. @JvmStatic
  255. @JvmOverloads
  256. fun finishActivity(mContext: Context, isTransition: Boolean = false) {
  257. removeActivity((mContext as Activity))
  258. if (isTransition) {
  259. mContext.onBackPressed()
  260. } else {
  261. mContext.finish()
  262. }
  263. }
  264. @JvmStatic
  265. fun fadeTransition(mContext: Context) {
  266. (mContext as Activity).overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
  267. }
  268. }