0

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.

Mahmoud Nabil
  • 243
  • 2
  • 12
  • cause may be you are doing too much things on main thread so kindly check it also check layout hierarchy. – Pratik Fagadiya Mar 05 '23 at 08:03
  • 2
    @PratikFagadiya -- I have 2 nested linear layouts, but that's important for the design. Thank you for replying. – Mahmoud Nabil Mar 05 '23 at 08:08
  • 1
    You're doing a lot of stuff in `onCreate` there, including what looks like downloading an emoji font, which is going to slow things down. I don't know how heavy all those libraries and their initialisations are, but that's what I'd look into - you can profile your app to see what's slowing it down: https://developer.android.com/studio/profile You might have to move some of this stuff off the main thread, or into coroutines, so you're not blocking the main thread (which is why you're seeing a frozen empty screen the user can't interact with) – cactustictacs Mar 05 '23 at 20:32

1 Answers1

1

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.

Younes Charfaoui
  • 1,059
  • 3
  • 10
  • 20