I'm starting in the world of android and kotlin, and I'm having a problem that when I click on an item in the bottom nav bar, instead of going through fragment, I close the app. I sent them the code of the classes and fragments that I generated to see if they can give me a hand.
LoginActivity
private fun attemptLogin() {
if (mAuthTask != null) {
return
}
// Reset errors.
email.error = null
password.error = null
// Store values at the time of the login attempt.
val emailStr = email.text.toString()
val passwordStr = password.text.toString()
var cancel = false
var focusView: View? = null
// Check for a valid password, if the user entered one.
if (!TextUtils.isEmpty(passwordStr) && !isPasswordValid(passwordStr)) {
password.error = getString(R.string.error_invalid_password)
focusView = password
cancel = true
}
// Check for a valid email address.
if (TextUtils.isEmpty(emailStr)) {
email.error = getString(R.string.error_field_required)
focusView = email
cancel = true
} else if (!isEmailValid(emailStr)) {
email.error = getString(R.string.error_invalid_email)
focusView = email
cancel = true
}
if (cancel) {
// There was an error; don't attempt login and focus the first
// form field with an error.
focusView?.requestFocus()
} else {
// Show a progress spinner, and kick off a background task to
// perform the user login attempt.
showProgress(true)
//mAuthTask = UserLoginTask(emailStr, passwordStr)
//mAuthTask!!.execute(null as Void?)
mAuth.signInWithEmailAndPassword(emailStr, passwordStr).addOnCompleteListener { task ->
if(task.isSuccessful){
startActivity(Intent(this, MainActivity::class.java))
this.finish()
}
else {
Toast.makeText(this, "Usuario o contraseña incorrectos,\nintente de nuevo",
Toast.LENGTH_LONG).show()
startActivity(Intent(this, LoginActivity::class.java))
this.finish()
}
}
}
}
MainActivity
class MainActivity: AppCompatActivity () {
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_profile -> {
message.setText(R.string.title_profile)
val profileFragment = ProfileFragment()
openFragment(profileFragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_map -> {
message.setText(R.string.title_map)
val mapFragment = MapFragment()
openFragment(mapFragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_notifications -> {
message.setText(R.string.title_notifications)
val notificationFragment = NotificationsFragment()
openFragment(notificationFragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_qr_scan -> {
message.setText(R.string.title_qr_scan)
val qrScanFragment = QrScanFragment()
openFragment(qrScanFragment)
return@OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
}
private fun openFragment(fragment: Fragment) {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.container, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack(null)
.commit()
}
}
NotificationFragment
class NotificationsFragment: Fragment () {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_notifications, container, false)
}