riku
2020-08-21 0328eab9276e57487eb2cfff31a945a01dd8c73a
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
package cn.flightfeather.thirdapp.module.base
 
 
import android.app.Activity
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import org.greenrobot.eventbus.EventBus
 
/**
 * @author riku
 * 2019.7.29
 * 基类fragment
 */
abstract class BaseFragment : Fragment() {
 
    var mContext: Context? = null
 
    var mRootView: ViewGroup? = null
 
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mRootView = container
        val view = inflater.inflate(getLayoutId(), container, false)
        onCreateView()
        return view
    }
 
    override fun onAttach(context: Context?) {
        super.onAttach(context)
        mContext = context
    }
 
    override fun onAttach(activity: Activity?) {
        super.onAttach(activity)
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            mContext = context
        }
    }
 
    override fun onDestroyView() {
        super.onDestroyView()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }
 
    open fun onCreateView() = Unit
 
    abstract fun getLayoutId(): Int
 
}