pull/51/head
kunfei 5 years ago
parent 15a8f6f0c8
commit bc665351ab
  1. 27
      app/src/main/java/io/legado/app/base/BaseFragment.kt

@ -1,29 +1,40 @@
package io.legado.app.base package io.legado.app.base
import android.view.Menu import android.os.Bundle
import android.view.MenuInflater import android.view.*
import android.view.MenuItem
import androidx.appcompat.view.SupportMenuInflater import androidx.appcompat.view.SupportMenuInflater
import androidx.appcompat.widget.Toolbar import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import io.legado.app.utils.applyTint import io.legado.app.utils.applyTint
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel import kotlinx.coroutines.Job
import kotlin.coroutines.CoroutineContext
abstract class BaseFragment(layoutID: Int) : Fragment(layoutID), abstract class BaseFragment(layoutID: Int) : Fragment(layoutID),
CoroutineScope by MainScope() { CoroutineScope {
lateinit var job: Job
var supportToolbar: Toolbar? = null var supportToolbar: Toolbar? = null
private set private set
val menuInflater: MenuInflater val menuInflater: MenuInflater
get() = SupportMenuInflater(requireContext()) get() = SupportMenuInflater(requireContext())
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
job = Job()
return super.onCreateView(inflater, container, savedInstanceState)
}
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
cancel() job.cancel()
} }
fun setSupportToolbar(toolbar: Toolbar) { fun setSupportToolbar(toolbar: Toolbar) {

Loading…
Cancel
Save