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
package cn.flightfeather.thirdapp.module.base
 
import android.content.Context
import android.content.Intent
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 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>()
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        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()
            }
        }
        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()
}