I have the following architecture in my project:
MainActivity
layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"
/>
</android.support.constraint.ConstraintLayout>
nav_graph
design:
nav_graph
xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/nav_graph"
app:startDestination="@id/loginFragment">
<fragment
android:id="@+id/loginFragment"
android:name="com.example.LoginFragment"
android:label="LoginFragment" >
<action
android:id="@+id/loginToContentAction"
app:destination="@id/contentFragment" />
</fragment>
<fragment
android:id="@+id/contentFragment"
android:name="com.example.ContentFragment"
android:label="ContentFragment" />
</navigation>
In the LoginFragment
, I have the following logic:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if(presenter.isUserLogged()) {
// getNav() returns NavController from the MainActivity
getNav().navigateTo(R.id.loginToContentAction)
return
}
// init login views etc
}
It works perfectly if the phone screen is turned ON, but (for example) if I deploy build via Android Studio, and phone screen is OFF, it will not navigate to ContentFragment
and stay in the LoginFragment
. I debugged the situation, and code enters NavController.navigate(R.id.loginToContentAction)
and steps into it, but it doesn't actually navigate. Any ideas what might be a cause of this?