1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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.v4.content.ContextCompat
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 android.widget.EditText
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()
        }
        getLayoutId()?.let {
            setContentView(it)
            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?
 
    /**
     * 拓展activity中自动显示软键盘的方法
     */
    fun autoShowKeyboard(editText: EditText, delay: Long = 500) {
        editText.postDelayed({
            ContextCompat.getSystemService(this, InputMethodManager::class.java)?.run {
                editText.requestFocus()
                showSoftInput(editText, 0)
            }
        }, delay)
    }
 
    /**
     * 拓展activity中隐藏软键盘的方法
     */
    fun hideKeyboard() {
        this.window?.peekDecorView()?.let {
            val inputManager =
                this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputManager.hideSoftInputFromWindow(it.windowToken, 0)
        }
    }
}
 
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()
}