Skip to content

Commit

Permalink
Fix direct share not working when activity doesn't exist
Browse files Browse the repository at this point in the history
Turns out that simply posting to main thread is not a good way to ensure
that we have a valid fragment object.

Closes #580
  • Loading branch information
oakkitten committed Nov 23, 2024
1 parent 5acaf7c commit 387ebef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
17 changes: 13 additions & 4 deletions app/src/main/java/com/ubergeek42/WeechatAndroid/WeechatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.
package com.ubergeek42.WeechatAndroid

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
Expand All @@ -40,6 +39,9 @@ import androidx.appcompat.widget.TooltipCompat
import androidx.core.content.ContextCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentManager.FragmentLifecycleCallbacks
import androidx.preference.PreferenceManager
import com.ubergeek42.WeechatAndroid.CutePagerTitleStrip.CutePageChangeListener
import com.ubergeek42.WeechatAndroid.adapters.BufferListClickListener
Expand All @@ -65,8 +67,8 @@ import com.ubergeek42.WeechatAndroid.service.Events.StateChangedEvent
import com.ubergeek42.WeechatAndroid.service.P
import com.ubergeek42.WeechatAndroid.service.RelayService
import com.ubergeek42.WeechatAndroid.service.getSystemTrustedCertificateChain
import com.ubergeek42.WeechatAndroid.service.showAlarmPermissionRationaleDialog
import com.ubergeek42.WeechatAndroid.service.shouldRequestExactAlarmPermission
import com.ubergeek42.WeechatAndroid.service.showAlarmPermissionRationaleDialog
import com.ubergeek42.WeechatAndroid.upload.Config
import com.ubergeek42.WeechatAndroid.upload.InsertAt
import com.ubergeek42.WeechatAndroid.upload.ShareObject
Expand Down Expand Up @@ -107,7 +109,7 @@ import java.security.cert.CertPathValidatorException
import java.security.cert.CertificateException
import java.security.cert.CertificateExpiredException
import java.security.cert.CertificateNotYetValidException
import java.util.*
import java.util.EnumSet
import javax.net.ssl.SSLPeerUnverifiedException
import kotlin.system.exitProcess

Expand Down Expand Up @@ -599,7 +601,14 @@ class WeechatActivity : AppCompatActivity(), CutePageChangeListener,
fragment.setShareObject(shareObject, InsertAt.END)
} else {
// let fragment be created first, if it's not ready
main { pagerAdapter.currentBufferFragment?.setShareObject(shareObject, InsertAt.END) }
supportFragmentManager.registerFragmentLifecycleCallbacks(object : FragmentLifecycleCallbacks() {
override fun onFragmentResumed(manager: FragmentManager, fragment: Fragment) {
if (fragment is BufferFragment && fragment.pointer == pointer) {
fragment.setShareObject(shareObject, InsertAt.END)
manager.unregisterFragmentLifecycleCallbacks(this)
}
}
}, false)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.*
import java.util.regex.PatternSyntaxException


Expand All @@ -106,7 +105,7 @@ interface BufferFragmentContainer {
class BufferFragment : Fragment(), BufferEye {
@Root private val kitty: Kitty = Kitty.make("BF")

private var pointer: Long = 0
var pointer: Long = 0

private var container: BufferFragmentContainer? = null
private var buffer: Buffer? = null
Expand Down

0 comments on commit 387ebef

Please sign in to comment.