Advertisement
GigaSer

Untitled

Jun 9th, 2021
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.97 KB | None | 0 0
  1. package ru.gigaser.hypersmm
  2.  
  3. import androidx.appcompat.app.AppCompatActivity
  4. import android.os.Bundle
  5. import android.util.Log
  6. import android.widget.TextView
  7. import android.widget.Toast
  8. import androidx.activity.viewModels
  9. import androidx.fragment.app.*
  10.  
  11. class LoginActivity : AppCompatActivity() {
  12.     private enum class LoginMode {
  13.         LOGIN, REGISTER
  14.     }
  15.  
  16.     private var mode = LoginMode.LOGIN
  17.  
  18.     override fun onCreate(savedInstanceState: Bundle?) {
  19.         super.onCreate(savedInstanceState)
  20.         setContentView(R.layout.activity_login)
  21.         val changeActionButton = findViewById<TextView>(R.id.register_text)
  22.         supportFragmentManager.addOnBackStackChangedListener {
  23.             mode = if (supportFragmentManager.backStackEntryCount == 0) {
  24.                 changeActionButton.text = getString(R.string.register_offer)
  25.                 LoginMode.LOGIN
  26.             } else {
  27.                 changeActionButton.text = getString(R.string.login_offer)
  28.                 LoginMode.REGISTER
  29.             }
  30.         }
  31.         changeActionButton.setOnClickListener {
  32.             if (mode == LoginMode.LOGIN) {
  33.                 supportFragmentManager.commit {
  34.                     setCustomAnimations(
  35.                     R.anim.slide_in,
  36.                     R.anim.fade_out,
  37.                     R.anim.fade_in,
  38.                     R.anim.slide_out
  39.                 )
  40.                     replace(R.id.fragment_container, RegisterFragment(), "Register")
  41.                     addToBackStack("Login")
  42.                 }
  43.  
  44.             } else {
  45.                 supportFragmentManager.popBackStack()
  46.             }
  47.         }
  48.         supportFragmentManager.setFragmentResultListener("loginRequestResult", this) { _, bundle ->
  49.             // We use a String here, but any type that can be put in a Bundle is supported
  50.             val result = bundle.getString("login")
  51.             Toast.makeText(applicationContext, result, Toast.LENGTH_SHORT).show()
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement