The chat activity of my application continues to view a white screen for one or two seconds after it has been destroyed, even if the app is not closed. I tried to put this line: <item name="android:statusBarColor">?attr/colorPrimaryVariant</item> into my app theme, but it's not working. Below are my onCreate(), and onStart() methods. If anyone can help me to improve the startup performance, thank you.

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityChatBinding.inflate(layoutInflater)
        setContentView(binding.root)
        when (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
            Configuration.UI_MODE_NIGHT_YES -> {}
            Configuration.UI_MODE_NIGHT_NO -> {}
        }
        //setSupportActionBar(chatToolbar)
        init()
        Handler().postDelayed({
            init()
        }, 10)
        audioRecordView = AudioRecordView()
        audioRecordView.initView(findViewById<View>(R.id.layoutMain) as FrameLayout)
        val containerView = audioRecordView.setContainerView(R.layout.layout_chatting)
        record = layoutInflater.inflate(R.layout.record_view, null)
        audioRecordView.recordingListener = this
        uName = containerView?.findViewById(R.id.chatUserName)!!
        uImage = containerView.findViewById(R.id.chatUserImg)!!
        uPresence = containerView.findViewById(R.id.chatUserPresence)
        recyclerView = containerView.findViewById(R.id.recyclerViewMessages)
        addContacts()

        EmojiManager.install(
            GoogleCompatEmojiProvider(
                EmojiCompat.init(
            FontRequestEmojiCompatConfig(
                this,
                FontRequest(
                    "com.google.android.gms.fonts",
                    "com.google.android.gms",
                    "Noto Color Emoji Compat",
                    R.array.com_google_android_gms_fonts_certs,
                )
            ).setReplaceAll(true)
                )
            )
        )
        //EmojiManager.install(FacebookEmojiProvider())
        //EmojiManager.install(TwitterEmojiProvider())
        val rootView = findViewById<View>(R.id.layoutMain)
        emojiPopup = EmojiPopup(rootView, audioRecordView.messageView!!)
        if (emojiPopup.isShowing){
            audioRecordView.emojiView?.setImageResource(R.drawable.ic_keyboard)
        } else {
            audioRecordView.emojiView?.setImageResource(R.drawable.emoji_ic)
        }

        //Record
        val filePath:String = externalCacheDir?.absolutePath + "/audioFile.wav"
        waveRecorder = WaveRecorder(filePath)

        waveRecorder.onStateChangeListener = {
            when (it) {
                RecorderState.RECORDING -> startRecording()
                RecorderState.STOP -> stopRecording()
                RecorderState.PAUSE -> pauseRecording()
            }
        }
        waveRecorder.onTimeElapsed = {
            Log.e(TAG, "onCreate: time elapsed $it")
        }

        //Chat
        messages = ArrayList()
        senderRoom = senderUid+receiverUid
        receiverRoom = receiverUid+senderUid
        setTxtDataToAdapter()
        //setImgDataToAdapter()
        setRecyclerView()
        //setListeners()
        setListener()
        //audioRecordView.messageView!!.requestFocus()
        val profileL = containerView.findViewById<View>(R.id.userImgNameL)
        val backBtn = containerView.findViewById<View>(R.id.single_ChatBack)
        profileL?.setOnClickListener{}
        backBtn?.setOnClickListener{ finish() }

        audioRecordView.setAttachmentOptions(AttachmentOption.defaultList, this)
        audioRecordView.removeAttachmentOptionAnimation(false)
        audioRecordView.removeAttachmentOptionAnimation(false)
    }
`override fun onStart() {
    super.onStart()
    val currentId = FirebaseAuth.getInstance().uid
    val presence = hashMapOf<String, Any>("presence" to "Online")
    val state = hashMapOf<String, Any>("state" to "available")
    firestore.collection("Users").document(currentId!!).update(presence)
    firestore.collection("Contacts").document(userId).collection("rooms")
        .document(currentId).update(state)
    val seen = hashMapOf<String, Any>("seen" to "true")
    firestore.collection("Chats").document(senderRoom!!)
        .collection("messages").document().update(seen)
    firestore.collection("Chats").document(receiverRoom!!)
        .collection("messages").document().update(seen)
}`

I tried to move the init() function to a background thread, but it doesn't work.

1

There are 1 best solutions below

1
Younes Charfaoui On

You are calling the init function twice; try to remove one, and I don't think you are using a different background thread when using the following code:

Handler().postDelayed({
            init()
        }, 10)

Since it will run on the same current thread, try to use Executors or some Kotlin coroutines for that!

Another thing you can try is moving the code that sets the presence and seen status to the onResume() method instead of onStart(). This will ensure that the code is executed only when the activity is visible to the user, which might improve the startup time of your app.