I am new in android, I created a alertdialog in my project that is basic and it has just [a message and one POSITIVE BUTTON and one NEGATIVE BUTTON] the message is a question that asks [Do you want to sign out from account?] and by pressing the NEGATIVE BUTTON closes the alertdialog, and by pressing the POSITIVE BUTTON it also closes the alertdialog and do nothing. I want to NEGATIVE BUTTON just close the alertdialog same as it does, but When I press the POSITIVE BUTTON it do two works for me [1- sign out from app and 2- start my activity which is called LoginActivity.kt]
Note : I already do that two works by another layout that is activity_sign_out.xml that contains a button for doing that two works. but I want to do that two works when alertdialog is showing just by pressing POSITIVE BUTTON.
I hope my question be clear for you.
I tryed to make that but it does'nt work, here is my codes :
1- my alertdialog :
import android.content.DialogInterface
import android.content.Intent
import android.content.res.Configuration
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.Toolbar
import androidx.drawerlayout.widget.DrawerLayout
import com.google.android.material.navigation.NavigationView
import com.google.firebase.auth.FirebaseAuth
import java.util.*
class DashboardActivity : AppCompatActivity() {
private lateinit var auth: FirebaseAuth
private lateinit var binding: ActivityDashboardBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
loadLocale()
binding = ActivityDashboardBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
val drawerLayout = findViewById<DrawerLayout>(R.id.drawerLayout)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
val navView = findViewById<NavigationView>(R.id.nav_view)
val toggle = ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close)
toggle.isDrawerIndicatorEnabled = true
supportActionBar?.setDisplayShowTitleEnabled(false)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
navView.setNavigationItemSelectedListener {
when(it.itemId){
R.id.itmSignOut ->startActivity(Intent(this,SignOut::class.java))
R.id.itmLanguage ->showChangeLanguageDialog()
R.id.itmProfile ->basicAlert()
}
true
}
}
private fun basicAlert(){
val positiveButtonClick = { _: DialogInterface, _: Int -> }
val negativeButtonClick = { _: DialogInterface, _: Int -> }
val builder = AlertDialog.Builder(this)
with(builder) {
setMessage(R.string.do_you_want_to_sign_out_from_your_my_conquer_account)
setPositiveButton(R.string.yes, ({ _: DialogInterface, _: Int ->
positiveButtonClick
auth.signOut()
startActivity(Intent(this,LoginActivity::class.java))
finish()
}))
setNegativeButton(R.string.no, negativeButtonClick)
show()
}
}
2- my SignOut.kt :
class SignOut : AppCompatActivity() {
private lateinit var auth: FirebaseAuth
private lateinit var binding: ActivitySignOutBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySignOutBinding.inflate(layoutInflater)
setContentView(R.layout.activity_sign_out)
val view = binding.root
setContentView(view)
initData()
}
private fun initData(){
auth = FirebaseAuth.getInstance()
setUserEmail()
clickListener()
}
private fun clickListener(){
binding.btnSignOut.setOnClickListener{
auth.signOut()
startActivity(Intent(this,LoginActivity::class.java))
finish()
}
}
private fun getCurrentUserEmail():String? {
return auth.currentUser?.email
}
@SuppressLint("SetTextI18n")
private fun setUserEmail(){
binding.tvUserEmail.text = "Welcome " getCurrentUserEmail()
}
}
3- and the activity_sign_out.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SignOut">
<TextView
android:id="@ id/tvUserEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome"
android:textAlignment="center"
android:textSize="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/btnSignOut"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:background="@drawable/custom_button"
android:text="@string/sign_out"
android:textAllCaps="false"
android:textColor="@color/White"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.8"
tools:ignore="TouchTargetSizeCheck,TextContrastCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
finally after running the app and coding for fixing it, I added the auth.signOut() startActivity(Intent(this,LoginActivity::class.java)) finish() to my Alertdialog but it shows the errors for red Intent : [None of the following functions can be called with the argument supplied.
- (Content!, Class<*>!)defined in android.content.Intent
- (String!, Uri!)defined in android.content.Intent]
thank you for helping me! take care.
CodePudding user response:
As you can see in Android Studio, this
within the with
block refers to the AlertDialog.Builder
instance. To refer to the Activity you must say so explictly:
setPositiveButton(R.string.yes) { _: DialogInterface, _: Int ->
auth.signOut()
startActivity(Intent(this@DashboardActivity, LoginActivity::class.java))
finish()
}