From cab7a7804485cc68e61a471789ad403026dce92f Mon Sep 17 00:00:00 2001 From: Ivan Kupalov Date: Mon, 28 Jan 2019 11:05:13 +0100 Subject: [PATCH] Fix crash in media tab, fix #1005 (#1007) --- .../java/com/keylesspalace/tusky/di/AppInjector.kt | 4 ++-- .../tusky/fragment/AccountMediaFragment.kt | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/di/AppInjector.kt b/app/src/main/java/com/keylesspalace/tusky/di/AppInjector.kt index 110131e4..495e8f30 100644 --- a/app/src/main/java/com/keylesspalace/tusky/di/AppInjector.kt +++ b/app/src/main/java/com/keylesspalace/tusky/di/AppInjector.kt @@ -17,6 +17,7 @@ package com.keylesspalace.tusky.di import android.app.Activity import android.app.Application +import android.content.Context import android.os.Bundle import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity @@ -68,8 +69,7 @@ object AppInjector { if (activity is FragmentActivity) { activity.supportFragmentManager.registerFragmentLifecycleCallbacks( object : FragmentManager.FragmentLifecycleCallbacks() { - override fun onFragmentCreated(fm: FragmentManager, f: Fragment, - savedInstanceState: Bundle?) { + override fun onFragmentPreAttached(fm: FragmentManager, f: Fragment, context: Context) { if (f is Injectable) { AndroidSupportInjection.inject(f) } diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/AccountMediaFragment.kt b/app/src/main/java/com/keylesspalace/tusky/fragment/AccountMediaFragment.kt index e5317c7e..37028e9f 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/AccountMediaFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/AccountMediaFragment.kt @@ -15,6 +15,7 @@ package com.keylesspalace.tusky.fragment +import android.content.Context import android.graphics.Color import android.os.Bundle import android.util.Log @@ -74,6 +75,7 @@ class AccountMediaFragment : BaseFragment(), Injectable { private var currentCall: Call>? = null private val statuses = mutableListOf() private var fetchingStatus = FetchingStatus.NOT_FETCHING + private var isVisibleToUser: Boolean = false private val callback = object : Callback> { override fun onFailure(call: Call>?, t: Throwable?) { @@ -187,12 +189,19 @@ class AccountMediaFragment : BaseFragment(), Injectable { } } }) + + if (isVisibleToUser) doInitialLoadingIfNeeded() } // That's sort of an optimization to only load media once user has opened the tab + // Attention: can be called before *any* lifecycle method! override fun setUserVisibleHint(isVisibleToUser: Boolean) { super.setUserVisibleHint(isVisibleToUser) - if (!isVisibleToUser) return + this.isVisibleToUser = isVisibleToUser + if (isVisibleToUser && isAdded) doInitialLoadingIfNeeded() + } + + private fun doInitialLoadingIfNeeded() { val accountId = arguments?.getString(ACCOUNT_ID_ARG) if (fetchingStatus == FetchingStatus.NOT_FETCHING && statuses.isEmpty()) { fetchingStatus = FetchingStatus.INITIAL_FETCHING