Issue
I have a LoginActivity in which there are two fragments. Using the navigation library, I switch between these fragments. But now I need to go from the fragment to another Activity. My code doesn't work for some reason, there are no errors in logCat. When I used this code with Activity everything works well. Is it all about a fragment or the navigation library?
My ViewModel
class AuthViewModel @Inject constructor(private var mAuth: FirebaseAuth) : ViewModel() {
var mEmail: String = ""
var mPassword: String = ""
var loginListener: LoginListener? = null
fun login(view: View) {
if (mEmail.isNotEmpty() && mPassword.isNotEmpty()) {
mAuth = FirebaseAuth.getInstance()
mAuth.signInWithEmailAndPassword(mEmail, mPassword).addOnCompleteListener {
if (it.isSuccessful) {
loginListener?.startLoading()
android.os.Handler().postDelayed({
loginListener?.endLoading()
loginListener?.validateLoginAndPassword()
}, 500)
} else {
loginListener?.showError(textResource = R.string.login_error)
}
}
} else {
loginListener?.showError(textResource = R.string.login_or_password_empty)
}
}
}
My Fragment
class LoginFragment : DaggerFragment(), View.OnClickListener, KeyboardVisibilityEventListener, LoginListener {
private lateinit var navController: NavController
lateinit var binding: FragmentLoginBinding
@Inject
lateinit var factory: ViewModelProvider.Factory
lateinit var mAuthViewModel: AuthViewModel
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
binding = FragmentLoginBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = Navigation.findNavController(view)
view.btn_registration.setOnClickListener(this)
mAuthViewModel = ViewModelProviders.of(this@LoginFragment, factory).get(AuthViewModel::class.java)
}
override fun onClick(view: View?) {
when(view!!.id) {
R.id.btn_registration -> navController.navigate(R.id.action_loginFragment_to_registerFragment)
}
}
override fun validateLoginAndPassword() {
//startActivity(Intent(activity, ContactListActivity::class.java))
val intent = Intent(activity, ContactListActivity::class.java)
startActivity(intent)
}
My LoginFragment XML
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="loginViewModel"
type="com.infernal93.phonebookappmvvmanddagger.viewmodels.AuthViewModel" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
tools:context=".view.fragments.LoginFragment">
<ScrollView
android:id="@+id/root_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true" >
<EditText
android:id="@+id/login_email"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"
android:background="@drawable/bg_inputs"
android:ems="10"
android:hint="@string/email_text"
android:text="@={loginViewModel.mEmail}"
android:inputType="textEmailAddress"
android:textColorHint="@color/dark_gray" />
<EditText
android:id="@+id/login_password"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_below="@id/login_email"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="@drawable/bg_inputs"
android:hint="@string/password_text"
android:text="@={loginViewModel.mPassword}"
android:inputType="textPassword"
android:textColorHint="@color/dark_gray" />
<com.github.rahatarmanahmed.cpv.CircularProgressView
android:id="@+id/cpv_login"
android:layout_width="@dimen/cpv_size"
android:layout_height="@dimen/cpv_size"
android:layout_below="@id/login_password"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:visibility="gone"
app:cpv_animAutostart="true"
app:cpv_color="@color/colorPrimary"
app:cpv_indeterminate="true" />
<Button
android:id="@+id/btn_login_enter"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_below="@id/cpv_login"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@drawable/bg_buttons"
android:text="@string/login_btn_text"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="@dimen/login_btns_text_size"
android:onClick="@{(view) -> loginViewModel.login(view)}"/>
<Button
android:id="@+id/btn_registration"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_below="@id/btn_login_enter"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@drawable/bg_buttons"
android:text="@string/registration_btn_text"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="@dimen/login_btns_text_size" />
</RelativeLayout>
</ScrollView>
</FrameLayout>
</layout>
Solution
I forgot to specify LoginListener in the fragment
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
//return inflater.inflate(R.layout.fragment_login, container, false)
binding = FragmentLoginBinding.inflate(inflater, container, false)
mAuthViewModel = ViewModelProviders.of(this@LoginFragment, factory).get(AuthViewModel::class.java)
binding.loginViewModel = mAuthViewModel
// LoginListener
mAuthViewModel.loginListener = this
return binding.root
}
Answered By - Infernal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.