package com.trust.ywx.utils

import android.app.Activity
import java.util.Stack

object AppManager {
    private val activityStack: Stack<Activity> = Stack()
    fun addActivity(activity: Activity) {
        activityStack.add(activity)
    }

    fun removeActivity(activity: Activity) {
        activityStack.remove(activity)
    }

    fun finishActivity(activity: Activity?): Activity? {
        if (activity != null) {
            removeActivity(activity)
            activity.finish()
            return activity
        } else {
            lastActivity()?.apply {
                removeActivity(this)
                this.finish()
                return this
            }

        }
        return null
    }

    fun lastActivity(): Activity? {
        return activityStack.lastElement()
    }
}