Я хочу переключиться с modifyfragment
на loginfragment
, нажав кнопку go_login_page_id
, поэтому я попытался вставить следующий код:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
go_login_page_id.setOnClickListener {
val intent = Intent(context, ModifyFragment::class.java)
startActivity(intent)
Log.d("MainActivity", "Try to show login activity")
}
login_id_re.setOnClickListener {
performRegister()
}
go_login_page_id.setOnClickListener {
var fr = fragmentManager?.beginTransaction()
fr?.replace(R.id.ModifyFragment, LoginFragment())
fr?.commit()
}
}
фрагмент register_activity.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:id="@+id/container_view_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/register_view_id"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
class="com.menu.a9dli3.ui.modifyFragment.ModifyFragment"
tools:ignore="MissingConstraints" />
<EditText
android:id="@+id/surname_id_re"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:ems="10"
android:hint="Surname"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="@+id/name_id"
app:layout_constraintStart_toStartOf="@+id/name_id"
app:layout_constraintTop_toBottomOf="@+id/name_id" />
<EditText
android:id="@+id/phone_id_re"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:ems="10"
android:hint="Phone"
android:inputType="phone"
app:layout_constraintEnd_toEndOf="@+id/surname_id_re"
app:layout_constraintStart_toStartOf="@+id/surname_id_re"
app:layout_constraintTop_toBottomOf="@+id/surname_id_re" />
<EditText
android:id="@+id/password_id_re"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="@+id/phone_id_re"
app:layout_constraintStart_toStartOf="@+id/phone_id_re"
app:layout_constraintTop_toBottomOf="@+id/phone_id_re" />
<Button
android:id="@+id/login_id_re"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:onClick="@string/menu_send"
android:text="Loging"
app:layout_constraintEnd_toEndOf="@+id/password_id_re"
app:layout_constraintStart_toStartOf="@+id/password_id_re"
app:layout_constraintTop_toBottomOf="@+id/password_id_re" />
<TextView
android:id="@+id/go_login_page_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="I Have an account"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="@+id/login_id_re"
app:layout_constraintStart_toStartOf="@+id/login_id_re"
app:layout_constraintTop_toBottomOf="@+id/login_id_re" />
<EditText
android:id="@+id/name_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="@+id/email_id_re"
app:layout_constraintStart_toStartOf="@+id/email_id_re"
app:layout_constraintTop_toBottomOf="@+id/email_id_re" />
<EditText
android:id="@+id/email_id_re"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="150dp"
android:ems="10"
android:hint="Email "
android:inputType="textEmailAddress"
android:padding="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Как видно из кода modifyFragment.kt :
package com.menu.a9dli3.ui.modifyFragment
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import com.menu.a9dli3.ui.login.LoginFragment
import kotlinx.android.synthetic.main.fragment_modify_profile.*
import android.R
class ModifyFragment: Fragment() {
private lateinit var modifyviewmodel: ModifyViewModel
override fun onCreateView (inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
{
modifyviewmodel = ViewModelProviders.of(this).get(ModifyViewModel::class.java)
val root = inflater.inflate(com.menu.a9dli3.R.layout.fragment_modify_profile, container, false)
val templatemode: TextView = root.findViewById(com.menu.a9dli3.R.id.text_slidemodify)
modifyviewmodel.text.observe( this, androidx.lifecycle.Observer { templatemode.text= it } )
return root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
login_id_re.setOnClickListener {
performRegister()
}
go_login_page_id.setOnClickListener {
var fr = fragmentManager?.beginTransaction()
fr?.replace(R.id.ModifyFragment, LoginFragment())
fr?.commit()
}
}
private fun performRegister() {
val email = email_id_re.text.toString()
val password = password_id_re.text.toString()
val name = surname_id_re.text.toString()
val surname = surname_id_re.toString()
val phone = phone_id_re.toString()
if (email.isEmpty() || password.isEmpty() || name.isEmpty() || phone.isEmpty() || surname.isEmpty()) {
Toast.makeText(context, "Please enter all the text fields", Toast.LENGTH_SHORT ).show()
return
}
Log.d("MainActivity", "Email is: $email")
Log.d("MainActivity", "Password: $password")
}
}
произошла ошибка неразрешенная ссылка ModifyFragment , я попытался вставить modify_list и login_list фрагменты в main_activity.xml в том смысле, что студия андроида предлагает мне эти фрагменты, но все те же вещи.
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="@+id/modify_list"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
class="com.menu.a9dli3.ui.modifyFragment.ModifyFragment"
/>
<fragment
android:id="@+id/login_list"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
class="com.menu.a9dli3.ui.login.LoginFragment"
/>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
Вы можете объяснить мне лучшие практики, касающиеся намерений и жизненного цикла для создания методов (onCreate, on viewCreated иonCreateview) в двух случаях Activity и фрагмент.