0

i tried to logged in the user, while waiting the auth i use a progress dialog to show the user there is a background process. I was getting a View not attached to window manager error message

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.adit.bangkit.plagroid, PID: 4238
java.lang.IllegalArgumentException: View=DecorView@9faec24[LoginActivity] not attached to window manager
    at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:524)
    at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:436)
    at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:136)
    at android.app.Dialog.dismissDialog(Dialog.java:387)
    at android.app.Dialog.dismiss(Dialog.java:369)
    at com.adit.bangkit.plagroid.ui.activity.BaseActivity.hideProgresDialog(BaseActivity.kt:46)
    at com.adit.bangkit.plagroid.ui.activity.user.LoginActivity.userLoggedInSuccess(LoginActivity.kt:160)
    at com.adit.bangkit.plagroid.firestore.FireStoreClass.getUsersDetails$lambda-6(fireStoreClass.kt:133)
    at com.adit.bangkit.plagroid.firestore.FireStoreClass.$r8$lambda$OiUXRq7QLpESvKkCxq8a6GYlIh4(Unknown Source:0)
    at com.adit.bangkit.plagroid.firestore.FireStoreClass$$ExternalSyntheticLambda1.onSuccess(Unknown Source:4)
    at com.google.android.gms.tasks.zzn.run(Unknown Source:4)
    at android.os.Handler.handleCallback(Handler.java:907)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7464)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955)

and this is my login activity

class LoginActivity : BaseActivity(), View.OnClickListener {
private lateinit var binding: ActivityLoginBinding
private lateinit var profil: SharedPreferences
private lateinit var userDetails: User
private lateinit var seller: Seller

private val firebaseAuth = FirebaseAuth.getInstance()
private val firebaseAuthListener = FirebaseAuth.AuthStateListener {
    val user = firebaseAuth.currentUser?.uid
    if (user != null){
        if (userDetails.userType == 0){
            val intent = Intent(this@LoginActivity, AdminActivity::class.java)
            startActivity(intent)
            finish()
        }
        else if (userDetails.userType == 1){
            val intent = Intent(this@LoginActivity, DashboardActivity::class.java)
            startActivity(intent)
            finish()
        }
        else if (seller.userType == 2){
            val intent = Intent(this@LoginActivity, SellerActivity::class.java)
            startActivity(intent)
            finish()
        }
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityLoginBinding.inflate(layoutInflater)
    setContentView(binding.root)

    supportActionBar?.hide()

    userDetails = User()
    seller = Seller()
    loginSession()

    binding.tvForgotPassword.setOnClickListener(this)
    binding.btnLogin.setOnClickListener(this)
    binding.tvRegister.setOnClickListener(this)
}

override fun onStart() {
    super.onStart()
    firebaseAuth!!.addAuthStateListener(this.firebaseAuthListener!!)
}

private fun loginSession(){
    profil = getSharedPreferences(Constants.PLAGRO_PREFERENCES, MODE_PRIVATE)
    if (profil.getString(Constants.EXTRA_USER_DETAILS, null) != null) {
        if(userDetails.userType == 0){
            val intent = Intent(this@LoginActivity, AdminActivity::class.java)
            intent.putExtra(Constants.EXTRA_USER_DETAILS, userDetails)
            startActivity(intent)
            finish()
        }else{
            if (userDetails.userType == 1){
                val intent = Intent(this@LoginActivity, DashboardActivity::class.java)
                intent.putExtra(Constants.EXTRA_USER_DETAILS, userDetails)
                startActivity(intent)
                finish()
            }else{
                if (seller.userType == 2){
                    val intent = Intent(this@LoginActivity, SellerActivity::class.java)
                    intent.putExtra(Constants.EXTRA_SELLER_DETAILS, seller)
                    startActivity(intent)
                    finish()
                }
            }
        }
    }
}

private fun validateLoginDetails():Boolean{
    return when{
        TextUtils.isEmpty(binding.etEmail.text.toString().trim {it <= ' '}) ->{
            showErrorSnackBar(resources.getString(R.string.err_msg_enter_email), true)
            false
        }

        TextUtils.isEmpty(binding.etPassword.text.toString().trim {it <= ' '}) ->{
            showErrorSnackBar(resources.getString(R.string.err_msg_enter_password), true)
            false
        }

        else ->{
            showErrorSnackBar(resources.getString(R.string.please_wait), false)
            true
        }
    }
}


private fun loginRegisteredUser(){

    if (validateLoginDetails()){

        showProgressDialog(resources.getString(R.string.please_wait))

        val email: String = binding.etEmail.text.toString().trim { it<=' ' }
        val password: String = binding.etPassword.text.toString().trim { it<=' ' }

        FirebaseAuth.getInstance().signInWithEmailAndPassword(email, password)
            .addOnCompleteListener { task ->

                if (task.isSuccessful) {
                    showErrorSnackBar(R.string.msg_login_success.toString(), false)
                    FireStoreClass().getUsersDetails(this@LoginActivity)
                } else {
                    hideProgresDialog()
                    showErrorSnackBar(task.exception!!.message.toString(), true)
                }
            }
    }
}


override fun onClick(view: View?){
    if (view != null){
        when(view.id){
            R.id.tv_forgot_password -> {
                val intent = Intent(this@LoginActivity, ForgotPasswordActivity::class.java)
                startActivity(intent)
            }
            R.id.btn_login -> {
                validateLoginDetails()
                loginRegisteredUser()
            }
            R.id.tv_register -> {
                val intent = Intent(this@LoginActivity, RegisterActivity::class.java)
                startActivity(intent)
            }
        }
    }
}


fun userLoggedInSuccess(user: User){
    //hide progress bar
    hideProgresDialog()

    //userType 0 = admin...... userType 1 = user....... userType 2 = seller
    if (user.userType == 0){
        val intent = Intent(this@LoginActivity, AdminActivity::class.java)
        intent.putExtra(Constants.EXTRA_USER_DETAILS, user)
        startActivity(intent)
    }else{
        if (user.profileComplete == 0){
            //jika profile user belum complete arahkan user ke activity UserProfileActivity
            val intent = Intent(this@LoginActivity, UserProfileActivity::class.java)
            intent.putExtra(Constants.EXTRA_USER_DETAILS, user)
            startActivity(intent)
        }else{
            //jika profile user sudah complete langsung arahkan ke MainActivity
            val intent = Intent(this@LoginActivity, DashboardActivity::class.java)
            intent.putExtra(Constants.EXTRA_USER_DETAILS, user)
            startActivity(intent)
        }
        finish()
    }
}
}

this is my base activity

open class BaseActivity : AppCompatActivity() {
private var doubleBackToExitPressedOnce = false
private lateinit var mProgressDialog: Dialog

fun showErrorSnackBar(message: String, errorMessage: Boolean){
    val snackbar = Snackbar.make(findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG)
    val snackbarView = snackbar.view

    if (errorMessage){
        snackbarView.setBackgroundColor(
            ContextCompat.getColor(this@BaseActivity, R.color.red))
    }else{
        snackbarView.setBackgroundColor(
            ContextCompat.getColor(this@BaseActivity, R.color.green))
    }
    snackbar.show()
}

fun showProgressDialog(text: String){
    mProgressDialog = Dialog(this)

    /*
    mengatur konten dari layout resource
    resource akan ......... , ditambahkan ke layer paling atas pada layar
     */
    mProgressDialog.setContentView(R.layout.dialog_progress)

    mProgressDialog.setCancelable(false)
    mProgressDialog.setCanceledOnTouchOutside(false)

    //menampilkan dialog di layar
    mProgressDialog.show()
}

fun hideProgresDialog(){
    mProgressDialog.dismiss()
}

fun doubleBackToExit(){
    if (doubleBackToExitPressedOnce){
        super.onBackPressed()
        return
    }

    this.doubleBackToExitPressedOnce = true

    Toast.makeText(this, resources.getString(R.string.please_click_back_again_to_exit),
        Toast.LENGTH_LONG).show()

    @Suppress("DEPRECATION")
    Handler().postDelayed({doubleBackToExitPressedOnce = false}, 2000)
}
}

last time that i run the program it run succesfully without error

Tri Aditya
  • 11
  • 3
  • Does this answer your question? [View not attached to window manager crash](https://stackoverflow.com/questions/22924825/view-not-attached-to-window-manager-crash) – ashwath hegde Apr 01 '22 at 17:15

0 Answers0