package cn.flightfeather.thirdappmodule.module.base
|
|
import android.annotation.SuppressLint
|
import android.app.Activity
|
import android.content.Context
|
import android.content.Intent
|
import android.content.pm.ActivityInfo
|
import android.os.Bundle
|
import android.support.v4.app.Fragment
|
import android.support.v4.app.FragmentManager
|
import android.support.v4.app.FragmentTransaction
|
import android.support.v7.app.AppCompatActivity
|
import android.support.v7.widget.Toolbar
|
import android.view.MenuItem
|
import android.view.ViewGroup
|
import android.view.inputmethod.InputMethodManager
|
import com.bumptech.glide.Glide
|
import io.reactivex.disposables.Disposable
|
import org.greenrobot.eventbus.EventBus
|
|
/**
|
* 基类Activity
|
* @author riku
|
* 2019.4.4
|
*/
|
abstract class BaseActivity : AppCompatActivity(), Toolbar.OnMenuItemClickListener {
|
|
|
lateinit var rootView: ViewGroup
|
|
protected val disposableList = mutableListOf<Disposable>()
|
|
@SuppressLint("SourceLockedOrientationActivity")
|
override fun onCreate(savedInstanceState: Bundle?) {
|
super.onCreate(savedInstanceState)
|
try {
|
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
|
} catch (e: Exception) {
|
e.printStackTrace()
|
}
|
setContentView(getLayoutId())
|
rootView = findViewById<ViewGroup>(android.R.id.content).getChildAt(0) as ViewGroup
|
}
|
|
override fun onDestroy() {
|
if (EventBus.getDefault().isRegistered(this)) {
|
EventBus.getDefault().unregister(this)
|
}
|
disposableList.forEach {
|
if (!it.isDisposed) {
|
it.dispose()
|
}
|
}
|
Glide.with(applicationContext).pauseRequests()
|
super.onDestroy()
|
}
|
|
override fun onMenuItemClick(p0: MenuItem?): Boolean{
|
return false
|
}
|
|
|
abstract fun getLayoutId(): Int
|
|
}
|
|
fun Context.startActivity(cls: Class<*>) {
|
startActivity(Intent(this, cls))
|
}
|
|
fun AppCompatActivity.addFragment(fragment: Fragment, fragmentId: Int) {
|
supportFragmentManager.inTransaction { add(fragmentId, fragment) }
|
}
|
|
fun AppCompatActivity.replaceFragment(fragment: Fragment, frameId: Int) {
|
supportFragmentManager.inTransaction { replace(frameId, fragment) }
|
}
|
|
inline fun FragmentManager.inTransaction(func: FragmentTransaction.() -> FragmentTransaction) {
|
beginTransaction().func().commit()
|
}
|
|
/**
|
* 拓展fragment中隐藏软键盘的方法
|
*/
|
fun Activity.hideKeyboard() {
|
this.window?.peekDecorView()?.let {
|
val inputManager =
|
this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
inputManager.hideSoftInputFromWindow(it.windowToken, 0)
|
}
|
}
|