我有一个使用新的Android Jetpack导航组件的新android应用程序。这是一个单独的活动MainActivity.kt
,其中包含导航主机片段。图表的起点是loginFragment
,并有一个动作action_log_in
导航到homeFragment
。基本结构如下:
由以下xml支持:
<navigation 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/nav_graph"
app:startDestination="@id/loginFragment">
<fragment
android:id="@+id/loginFragment"
android:name="io.foobar.backtobasics.ui.pages.login.LoginFragment"
android:label="LoginFragment" >
<action
android:id="@+id/action_log_in"
app:destination="@id/homeFragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:launchSingleTop="false"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim"
app:popUpTo="@+id/homeFragment"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/homeFragment"
android:name="io.foobar.backtobasics.ui.pages.home.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" />
</navigation>
在使用“有效”凭据单击登录按钮后,应将用户导航到主屏幕。调用findNavController().navigate(R.id.action_log_in)
时,没有任何反应。登录屏幕仍然可见。我删除了一些断点,发现HomeFragment
已创建,但没有显示。登录片段仍然可见。当我尝试对Navigation调用进行变体时,但似乎没有任何效果。有人以前见过这样的行为吗?
以下是主要活动的Kotlin / Layout和两个片段。
MainActivity.kt
class MainActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navController = findNavController(R.id.nav_host_fragment)
findViewById<BottomNavigationView>(R.id.navigation).setupWithNavController(navController)
}
}
activity_main.xml
<layout>
<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.pages.home.MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
LoginFragment.kt
class LoginFragment: Fragment() {
private lateinit var vm: LoginViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setupBindings(savedInstanceState)
}
private fun setupBindings(savedInstanceState: Bundle?) {
val binding: ActivityLoginBinding = DataBindingUtil.setContentView(requireActivity(), R.layout.activity_login)
vm = ViewModelProviders.of(this@LoginFragment).get(LoginViewModel::class.java)
if (savedInstanceState == null) vm = LoginViewModel(requireActivity().application)
binding.vm = vm
binding.executePendingBindings()
setUpLoginButton()
}
private fun setUpLoginButton() {
vm.getLoginClick().observe(this, Observer<LoginFields>{
fields: LoginFields ->
this.findNavController().navigate(R.id.homeFragment)
})
}
}
fragment_login.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="vm"
type="io.foobar.backtobasics.ui.pages.login.LoginViewModel"/>
</data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/title_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="top|center_horizontal"
android:text="@string/label_title"
android:textColor="@color/colorPrimary"
android:textSize="60sp" />
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="5dp"
android:hint="@string/hint_username">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:text="@={vm.login.email}"
app:error="@{vm.login.emailError}"
app:onfocus="@{vm.getEmailOnFocusChangeListener()}" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:hint="@string/hint_password">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:text="@={vm.login.password}"
app:error="@{vm.login.passwordError}"
app:onfocus="@{vm.getPasswordOnFocusChangeListener()}" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/button"
style="@style/Widget.MaterialComponents.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="@{vm.login.valid}"
android:onClick="@{() -> vm.onLoginClicked()}"
android:text="@string/button_login" />
</LinearLayout>
</layout>
HomeFragment.kt
class HomeFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
}
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.pages.home.HomeFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/title_home" />
</FrameLayout>