Modify:增加fragment,activity重建判断,增加fragment重建示例页面。
This commit is contained in:
@ -7,6 +7,7 @@ import com.alibaba.android.arouter.launcher.ARouter
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.BindingReflex
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusRegister
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusUtils
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.ViewRecreateHelper
|
||||
|
||||
/**
|
||||
* Activity基类
|
||||
@ -20,9 +21,16 @@ abstract class BaseFrameActivity<VB : ViewBinding> : AppCompatActivity(), FrameV
|
||||
BindingReflex.reflexViewBinding(javaClass, layoutInflater)
|
||||
}
|
||||
|
||||
/**
|
||||
* activity页面重建帮助类
|
||||
*/
|
||||
private var mStatusHelper: ActivityRecreateHelper? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(mBinding.root)
|
||||
//处理保存的装填
|
||||
mStatusHelper?.onRestoreInstanceStatus(savedInstanceState)
|
||||
// ARouter 依赖注入
|
||||
ARouter.getInstance().inject(this)
|
||||
// 注册EventBus
|
||||
@ -38,4 +46,22 @@ abstract class BaseFrameActivity<VB : ViewBinding> : AppCompatActivity(), FrameV
|
||||
)
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
override fun isRecreate(): Boolean = mStatusHelper?.isRecreate ?: false
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
if (mStatusHelper == null) {
|
||||
//仅当触发重建需要保存状态时创建对象
|
||||
mStatusHelper = ActivityRecreateHelper(outState)
|
||||
} else {
|
||||
mStatusHelper?.onSaveInstanceState(outState)
|
||||
}
|
||||
super.onSaveInstanceState(outState)
|
||||
}
|
||||
|
||||
/**
|
||||
* - activity 重建帮助工具类
|
||||
*/
|
||||
private class ActivityRecreateHelper(savedInstanceState: Bundle? = null) : ViewRecreateHelper(savedInstanceState)
|
||||
|
||||
}
|
||||
@ -10,6 +10,7 @@ import com.alibaba.android.arouter.launcher.ARouter
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.BindingReflex
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusRegister
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusUtils
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.ViewRecreateHelper
|
||||
|
||||
/**
|
||||
* Fragment基类
|
||||
@ -23,6 +24,11 @@ abstract class BaseFrameFragment<VB : ViewBinding> : Fragment(), FrameView<VB> {
|
||||
BindingReflex.reflexViewBinding(javaClass, layoutInflater)
|
||||
}
|
||||
|
||||
/**
|
||||
* fragment状态保存工具类
|
||||
*/
|
||||
private var mStatusHelper: FragmentStatusHelper? = null
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
@ -33,6 +39,8 @@ abstract class BaseFrameFragment<VB : ViewBinding> : Fragment(), FrameView<VB> {
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
//处理恢复
|
||||
mStatusHelper?.onRestoreInstanceStatus(savedInstanceState)
|
||||
// ARouter 依赖注入
|
||||
ARouter.getInstance().inject(this)
|
||||
// 注册EventBus
|
||||
@ -42,10 +50,31 @@ abstract class BaseFrameFragment<VB : ViewBinding> : Fragment(), FrameView<VB> {
|
||||
initRequestData()
|
||||
}
|
||||
|
||||
override fun isRecreate(): Boolean = mStatusHelper?.isRecreate ?: false
|
||||
|
||||
/**
|
||||
* 页面可能重建的时候回执行此方法,进行当前页面状态保存
|
||||
*/
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
if (mStatusHelper == null) {
|
||||
//仅当触发重建需要保存状态时创建对象
|
||||
mStatusHelper = FragmentStatusHelper(outState)
|
||||
} else {
|
||||
mStatusHelper?.onSaveInstanceState(outState)
|
||||
}
|
||||
super.onSaveInstanceState(outState)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
if (javaClass.isAnnotationPresent(EventBusRegister::class.java)) EventBusUtils.unRegister(
|
||||
this
|
||||
)
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
/**
|
||||
* - fragment状态保存帮助类;
|
||||
* - 暂时没有其他需要保存的--空继承
|
||||
*/
|
||||
private class FragmentStatusHelper(savedInstanceState: Bundle? = null) : ViewRecreateHelper(savedInstanceState)
|
||||
}
|
||||
@ -20,7 +20,12 @@ interface FrameView<VB : ViewBinding> {
|
||||
fun initLiveDataObserve()
|
||||
|
||||
/**
|
||||
* 初始化界面创建时的数据请求
|
||||
* 初始化界面创建时的数据请求,尝试在此方法内调用[isRecreate]进行重建判断,防止数据重复获取
|
||||
*/
|
||||
fun initRequestData()
|
||||
|
||||
/**
|
||||
* 页面是否重建,fragment被回收重新展示的时候为true,系统环境发生变化activity重新创建时为true
|
||||
*/
|
||||
fun isRecreate(): Boolean
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.base.utils
|
||||
|
||||
import android.os.Bundle
|
||||
|
||||
/**
|
||||
* @author DBoy 2021/7/8
|
||||
*
|
||||
* - 文件描述 : 视图,activity,fragment重建帮助类
|
||||
*/
|
||||
open class ViewRecreateHelper(savedInstanceState: Bundle?=null) {
|
||||
/**
|
||||
* 重建标记key
|
||||
*/
|
||||
private val KEY_RECREATE = "recreate"
|
||||
|
||||
/**
|
||||
* 是否重建
|
||||
*/
|
||||
var isRecreate = false
|
||||
private set
|
||||
|
||||
init {
|
||||
if (savedInstanceState!=null) {
|
||||
this.onSaveInstanceState(savedInstanceState)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 恢复状态
|
||||
*/
|
||||
open fun onRestoreInstanceStatus(savedInstanceState: Bundle?) {
|
||||
isRecreate = savedInstanceState?.getBoolean(KEY_RECREATE) ?: false
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存状态
|
||||
*/
|
||||
open fun onSaveInstanceState(bundle: Bundle) {
|
||||
bundle.putBoolean(KEY_RECREATE, true)
|
||||
}
|
||||
|
||||
}
|
||||
@ -8,5 +8,11 @@ package com.quyunshuo.androidbaseframemvvm.net
|
||||
*/
|
||||
internal object NetBaseUrlConstant {
|
||||
|
||||
const val MAIN_URL = ""
|
||||
val MAIN_URL = "https://www.baidu.com"
|
||||
get() {
|
||||
if (field.isEmpty()){
|
||||
throw NotImplementedError("请求改你的 MAIN_URL 的值为自己的请求地址")
|
||||
}
|
||||
return field
|
||||
}
|
||||
}
|
||||
@ -11,6 +11,7 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity android:name=".activity.InternalPagerActivity" />
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@ -1,9 +1,12 @@
|
||||
package com.quyunshuo.module.home
|
||||
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
import androidx.activity.viewModels
|
||||
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseActivity
|
||||
import com.quyunshuo.module.home.activity.InternalPagerActivity
|
||||
import com.quyunshuo.module.home.databinding.HomeActivityMainBinding
|
||||
import com.quyunshuo.module.home.fragment.InternalFragment
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
|
||||
/**
|
||||
@ -20,9 +23,14 @@ class MainActivity : BaseActivity<HomeActivityMainBinding>() {
|
||||
*/
|
||||
private val mViewModel by viewModels<HomeViewModel>()
|
||||
|
||||
override fun HomeActivityMainBinding.initView() {}
|
||||
override fun HomeActivityMainBinding.initView() {
|
||||
goToNextBtn.setOnClickListener {
|
||||
startActivity(Intent(this@MainActivity, InternalPagerActivity::class.java))
|
||||
}
|
||||
}
|
||||
|
||||
override fun initLiveDataObserve() {
|
||||
|
||||
// 订阅数据
|
||||
mViewModel.data.observe(this, {
|
||||
mBinding.vTvHello.text = it
|
||||
|
||||
@ -0,0 +1,46 @@
|
||||
package com.quyunshuo.module.home.activity
|
||||
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.viewpager2.adapter.FragmentStateAdapter
|
||||
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseActivity
|
||||
import com.quyunshuo.module.home.databinding.HomeActivityInternalLayoutBinding
|
||||
import com.quyunshuo.module.home.fragment.InternalFragment
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
|
||||
/**
|
||||
* @author DBoy 2021/7/6 <p>
|
||||
* - 文件描述 : ViewPager2+fragment 模拟Fragment页面重建。
|
||||
*/
|
||||
@AndroidEntryPoint
|
||||
class InternalPagerActivity : BaseActivity<HomeActivityInternalLayoutBinding>() {
|
||||
|
||||
override fun HomeActivityInternalLayoutBinding.initView() {
|
||||
initPager()
|
||||
}
|
||||
|
||||
private fun initPager() {
|
||||
val fragments = mutableListOf<Fragment>(
|
||||
InternalFragment(),
|
||||
InternalFragment(),
|
||||
InternalFragment(),
|
||||
InternalFragment(),
|
||||
InternalFragment(),
|
||||
InternalFragment(),
|
||||
InternalFragment()
|
||||
)
|
||||
mBinding.viewPager.adapter = object : FragmentStateAdapter(this) {
|
||||
override fun getItemCount(): Int = fragments.size
|
||||
override fun createFragment(position: Int): Fragment = fragments[position]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override fun initLiveDataObserve() {
|
||||
|
||||
}
|
||||
|
||||
override fun initRequestData() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,48 @@
|
||||
package com.quyunshuo.module.home.fragment
|
||||
|
||||
import androidx.fragment.app.viewModels
|
||||
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseFragment
|
||||
import com.quyunshuo.module.home.databinding.HomeFragmentInternalLayoutBinding
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
|
||||
/**
|
||||
* @author DBoy 2021/7/6 <p>
|
||||
* - 文件描述 : 测试fragment
|
||||
*/
|
||||
@AndroidEntryPoint
|
||||
class InternalFragment : BaseFragment<HomeFragmentInternalLayoutBinding>() {
|
||||
|
||||
private val mViewModel by viewModels<InternalViewModel>()
|
||||
|
||||
override fun HomeFragmentInternalLayoutBinding.initView() {
|
||||
|
||||
}
|
||||
|
||||
override fun initLiveDataObserve() {
|
||||
mViewModel.recreatedCont.observe(viewLifecycleOwner) {
|
||||
mBinding.recreateContTv.text = "重建次数 $it"
|
||||
}
|
||||
mViewModel.firstData.observe(viewLifecycleOwner) {
|
||||
mBinding.loadDataTv.text = it
|
||||
}
|
||||
mViewModel.isLoading.observe(viewLifecycleOwner) {
|
||||
mBinding.loadingStatusTv.text = if (it) {
|
||||
"正在加载..."
|
||||
} else {
|
||||
"加载完成!"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun initRequestData() {
|
||||
//每次重建都会累加数据
|
||||
mViewModel.increase()
|
||||
//当页面重建的时候不再重新请求数据,且当前页面数据数据有且没有刷新逻辑的情况下不再请求数据。
|
||||
if (isRecreate() && mViewModel.firstData.value != null) {
|
||||
return
|
||||
}
|
||||
mViewModel.getData()
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
package com.quyunshuo.module.home.fragment
|
||||
|
||||
import com.quyunshuo.androidbaseframemvvm.base.mvvm.m.BaseRepository
|
||||
import kotlinx.coroutines.delay
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* @author DBoy 2021/7/6 <p>
|
||||
* - 文件描述 :
|
||||
*/
|
||||
class InternalRepository @Inject constructor() : BaseRepository() {
|
||||
|
||||
suspend fun getData() = flowRequest<String> {
|
||||
delay(1000)
|
||||
emit("数据加载成功")
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,62 @@
|
||||
package com.quyunshuo.module.home.fragment
|
||||
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* @author DBoy 2021/7/6 <p>
|
||||
* - 文件描述 :
|
||||
*/
|
||||
@HiltViewModel
|
||||
class InternalViewModel @Inject constructor(private val repository: InternalRepository) : BaseViewModel() {
|
||||
|
||||
/**
|
||||
* 重建计数
|
||||
*/
|
||||
val recreatedCont = MutableLiveData<Int>()
|
||||
|
||||
/**
|
||||
* 首个数据
|
||||
*/
|
||||
val firstData = MutableLiveData<String>()
|
||||
|
||||
/**
|
||||
* 累加重建次数
|
||||
*/
|
||||
fun increase() {
|
||||
val value = recreatedCont.value ?: 0
|
||||
recreatedCont.value = value + 1
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
*/
|
||||
fun getData() {
|
||||
isLoading.value = true
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
repository.getData()
|
||||
.catch {
|
||||
Log.d("DJC", "getData: ")
|
||||
}.collect {
|
||||
isLoading.postValue(false)
|
||||
delay(200)
|
||||
firstData.postValue(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
Log.d("DJC","InternalViewModel Clear")
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
|
||||
<androidx.viewpager2.widget.ViewPager2
|
||||
android:id="@+id/viewPager"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
@ -20,4 +20,18 @@
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/goToNextBtn"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="30dp"
|
||||
android:text="进入ViewPager2示例"
|
||||
android:textAllCaps="false"
|
||||
android:textSize="20sp"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/vTvHello"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center"
|
||||
android:orientation="vertical">
|
||||
|
||||
|
||||
<TextView
|
||||
android:id="@+id/loadingStatusTv"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textColor="#333"
|
||||
android:textSize="28sp"
|
||||
tools:text="Loging" />
|
||||
|
||||
|
||||
<TextView
|
||||
android:id="@+id/loadDataTv"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="20dp"
|
||||
android:textColor="#333"
|
||||
android:textSize="28sp"
|
||||
tools:text="数据" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/recreateContTv"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="50dp"
|
||||
android:textColor="#333"
|
||||
android:textSize="28sp"
|
||||
tools:text="重建计数" />
|
||||
|
||||
</androidx.appcompat.widget.LinearLayoutCompat>
|
||||
Reference in New Issue
Block a user