Skip to content

Commit

Permalink
[优化] 在fragment的resume阶段响应事件
Browse files Browse the repository at this point in the history
  • Loading branch information
YenalyLiew committed Oct 15, 2024
1 parent b8eace8 commit 9219acd
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class MainActivity : YenalyActivity<ActivityMainBinding>(), DrawerListener {
)
}

override val fragmentOnAttachListener: (Fragment) -> Unit = { fragment ->
override val onFragmentResumedListener: (Fragment) -> Unit = { fragment ->
logScreenViewEvent(fragment)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class SearchActivity : YenalyActivity<ActivitySearchBinding>(), StateLayoutMixin
override fun getViewBinding(layoutInflater: LayoutInflater): ActivitySearchBinding =
ActivitySearchBinding.inflate(layoutInflater)

override val fragmentOnAttachListener: (Fragment) -> Unit = { fragment ->
override val onFragmentResumedListener: (Fragment) -> Unit = { fragment ->
logScreenViewEvent(fragment)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SettingsActivity : YenalyActivity<ActivitySettingsBinding>() {
override fun getViewBinding(layoutInflater: LayoutInflater): ActivitySettingsBinding =
ActivitySettingsBinding.inflate(layoutInflater)

override val fragmentOnAttachListener: (Fragment) -> Unit = { fragment ->
override val onFragmentResumedListener: (Fragment) -> Unit = { fragment ->
logScreenViewEvent(fragment)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class VideoActivity : YenalyActivity<ActivityVideoBinding>(),
override fun getViewBinding(layoutInflater: LayoutInflater): ActivityVideoBinding =
ActivityVideoBinding.inflate(layoutInflater)

override val fragmentOnAttachListener: (Fragment) -> Unit = { fragment ->
override val onFragmentResumedListener: (Fragment) -> Unit = { fragment ->
logScreenViewEvent(fragment)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.MenuProvider
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentOnAttachListener
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.navigation.fragment.NavHostFragment
Expand Down Expand Up @@ -63,29 +62,25 @@ abstract class FrameActivity : AppCompatActivity() {
}

/**
* 能够监听该 Activity 旗下所有 Fragment 的 onAttach 事件
* 能够监听该 Activity 旗下所有 Fragment 的 onResume 事件
*/
open val fragmentOnAttachListener: ((Fragment) -> Unit)? = null
open val onFragmentResumedListener: ((Fragment) -> Unit)? = null

override fun onCreate(savedInstanceState: Bundle?) {
setUiStyle()
super.onCreate(savedInstanceState)
if (fragmentOnAttachListener != null) {
supportFragmentManager.addFragmentOnAttachListener(object : FragmentOnAttachListener {
override fun onAttachFragment(fm: FragmentManager, fragment: Fragment) {
if (fragment is NavHostFragment) {
fm.removeFragmentOnAttachListener(this)
fragment.childFragmentManager.addFragmentOnAttachListener(this)
return
}
fragmentOnAttachListener?.invoke(fragment)
Log.d("FrameActivity", "onAttachFragment: $fragment")
if (onFragmentResumedListener != null) {
supportFragmentManager.registerFragmentLifecycleCallbacks(object :
FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentResumed(fm: FragmentManager, f: Fragment) {
if (f is NavHostFragment) return
onFragmentResumedListener?.invoke(f)
Log.d("FrameActivity", "onFragmentResumed: $f")
}
})
}, true)
}
}


override fun onDestroy() {
super.onDestroy()
if (this::loadingDialog.isInitialized) {
Expand Down

0 comments on commit 9219acd

Please sign in to comment.