Talkback doesn't focus new fragment after second navigation - android

I have an app using Android Navigation (single activity many fragments).
When launched, the app displays a loading screen, an optional "news"-type screen which returns the user to the home screen when they're done, then a home screen.
However, I'm having trouble getting accessibility to pick up the home screen. When the news screen isn't shown, it'll get focused fine, but when it is included, the home screen will not receive focus, which seems to remain on the loading screen.
I've been able to reproduce this in a minimal example, with screens "Loading", "A", and "B". The app behaviour here is:
Launch on Main Fragment
wait 5 seconds, navigate to fragment A
user presses "continue"
return to Main Fragment
Wait 5 seconds, navigate to fragment B
After each navigation, I'd expect the text element to be focused, and read out. However, for fragment B, it isn't.
Things I've tried:
Using <requestFocus /> in fragment B
Requesting focus in code
Setting the importantForAccessibility state of the "Loading" fragment just before navigating away
If I change the loading fragment to use buttons for navigation, instead of the automatic behaviour, the focus does pass correctly, but obviously doesn't provide the right experience.
class LoadingFragment : Fragment() {
private lateinit var rootView: View
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View = inflater.inflate(R.layout.fragment_main, container, false).also {
rootView = it
}
private var beenHereBefore = false
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Mimic a delayed loading process
Handler(Looper.getMainLooper()).postDelayed({
// Navigate to the correct screen, based on state
findNavController().navigate(
if (beenHereBefore) {
R.id.action_mainFragment_to_fragmentB
} else {
beenHereBefore = true
R.id.action_mainFragment_to_fragmentA
}
)
}, 5000)
}
}
fragment_main.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/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.LoadingFragment">
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MainFragment"
android:textColor="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
class FragmentA : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View = inflater.inflate(R.layout.fragment_a, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.button).setOnClickListener {
findNavController().popBackStack()
}
}
}
fragment_a.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:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="308dp"
android:text="Continue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
class FragmentB : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View = inflater.inflate(R.layout.fragment_b, container, false)
}
fragment_b.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:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment B"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
main_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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_graph"
app:startDestination="#id/mainFragment">
<fragment
android:id="#+id/mainFragment"
android:name="com.example.navigationtestapp.ui.main.LoadingFragment"
android:label="fragment_main"
tools:layout="#layout/fragment_main" >
<action
android:id="#+id/action_mainFragment_to_fragmentA"
app:destination="#id/fragmentA" />
<action
android:id="#+id/action_mainFragment_to_fragmentB"
app:destination="#id/fragmentB" />
</fragment>
<fragment
android:id="#+id/fragmentA"
android:name="com.example.navigationtestapp.ui.main.FragmentA"
android:label="FragmentA" />
<fragment
android:id="#+id/fragmentB"
android:name="com.example.navigationtestapp.ui.main.FragmentB"
android:label="FragmentB" />
</navigation>

If I were you will try to handle the accessibility focus using the accessibility API and not the requestFocus. You've mentioned that the the focus seems to be held by the loading screen, so this should work due that the a11y focus will follow it, but I will try to handle that using the sendAccessibilityEvent method
https://developer.android.com/reference/android/view/accessibility/AccessibilityEventSource#sendAccessibilityEvent(int)
// Moving a11y focus from btn1 to btn 2
btn1 = (Button) findViewById(R.id.screen3_btn1);
btn2 = (Button) findViewById(R.id.screen3_btn2);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn2.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
}
});
Excellent answer about this: Android - Set TalkBack accessibility focus to a specific view
Hope this helps

Related

Set a Fragment inside a Fragment

I have seen similar questions here, but none of them helps.
I have MainActivity and a few Fragments which works fine, but I want to set one fragment inside of another.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomMenu)
val hostFragment = supportFragmentManager.findFragmentById(R.id.fragmentContainer) as NavHostFragment
val navigationController = hostFragment.navController
bottomNavigationView.setupWithNavController(navigationController)
setupActionBarWithNavController(navigationController)
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.fragmentContainer)
return navController.navigateUp() || super.onSupportNavigateUp()
}
}
Parent Fragment:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_main, container, false)
incomeButton = view.findViewById(R.id.addIncomeButton)
expenseButton = view.findViewById(R.id.addExpenseButton)
val childFragment: Fragment = ChartFragment()
val transaction: FragmentTransaction = childFragmentManager.beginTransaction()
transaction.replace(R.id.childFragment, childFragment).commit()
return view
}
Child Fragment:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
return ComposeView(requireContext()).apply {
setContent {
Text(
text = "here is another important code",
fontSize = 28.sp
)
}
}
The layouts for Activity:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomMenu"
android:layout_width="match_parent"
android:layout_height="60dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:menu="#menu/bottom_menu" />
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainer"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="475dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#+id/bottomMenu"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_map" />
</androidx.constraintlayout.widget.ConstraintLayout>
The layouts for Fragment:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.MainFragment">
<Button
android:id="#+id/addIncomeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="21dp"
android:layout_marginBottom="21dp"
android:backgroundTint="#color/primary"
android:text="#string/addIncomeButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="#+id/addExpenseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="21dp"
android:layout_marginBottom="10dp"
android:backgroundTint="#color/red"
android:text="#string/addExpenseButton"
app:layout_constraintBottom_toTopOf="#+id/addIncomeButton"
app:layout_constraintEnd_toEndOf="parent" />
<FrameLayout
android:id="#+id/forChart"
android:layout_width="match_parent"
android:layout_height="350dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The most of the same questions' solvations say to use childFragmentManager so do I but it doesn't work.
I get an error java.lang.IllegalArgumentException: No view found for id 0x7f08023f.
I guess I do not understand something, but I can't realize what.
Thank you!
Seems that you're mixing the ids of the container for the transaction - that's also what the error says. You're trying to put the ChartFragment into a container with R.id.childFragment id:
transaction.replace(R.id.childFragment, childFragment).commit()
And from what I see in the MainFragment layout, the container id is R.id.forChart. Try changing it to this:
transaction.replace(R.id.forChart, childFragment).commit()

Why is navigation destination lost when I launch a action for the second time with navigation in Android Studio?

I use navigation in my project.
The Image 1 will be displayed when the app start, I click Next button in Image 1, the Image 2 will be displayed, then I click Back button in Image 2, the image 1 will be displayed again.
Now I click Next button in Image 1 again, the app crash, I get the error 'navigation destination com.example.myapplication:id/actionTwo is unknown to this NavController', why?
Why can't NavHostFragment.findNavController(this).navigate(OneFrDirections.actionTwo()) be launched for the second time ?
You can download the test code at https://www.dropbox.com/s/1ad47eqi4iwig5v/MyNAV.zip?dl=0
my.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/my"
app:startDestination="#+id/One">
<fragment
android:id="#+id/One"
android:name="com.example.myapplication.OneFr"
android:label="One">
<action
android:id="#+id/actionTwo"
app:destination="#id/Two" />
</fragment>
<fragment
android:id="#+id/Two"
android:name="com.example.myapplication.TwoFr"
android:label="Two">
<action
android:id="#+id/actionOne"
app:destination="#id/One" />
</fragment>
</navigation>
OneFr.kt
class OneFr : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.one, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.btnNext).setOnClickListener {
NavHostFragment.findNavController(this).navigate(OneFrDirections.actionTwo())
}
}
}
TwoFr.kt
class TwoFr : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.two, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Handle back button press
view.findViewById<Button>(R.id.btnBack).setOnClickListener {
fragmentManager?.popBackStack()
}
}
}
one.xml
<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:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
two.xml
<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:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
tools:context=".MainActivity">
<fragment
android:id="#+id/fragment_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/my" />
</FrameLayout>
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Image 1
Image 2
Instead of fragmentManager?.popBackStack() , try
NavHostFragment.findNavController(this).popBackStack()
Use NavigationController for back stack since NavController is managing navigation, manually popping fragments would cause errors in NavController.
Try the following for navigating:
From FrOne to FrTwo:
view.findNavController().navigate(R.id.actionTwo)
From FrTwo to FrOne:
view.findNavController().navigate(R.id.actionOne)

Can not Click the Items in Fragment

I'm using old way to create bottom navigation and using Fragment as well.
When I starting design some view inside Fragment. Like Edit, TextBox or ScollView etc. Everything in Fragment are unclickable.
(I had run the app on real device and emulator.
I can not use my mouse click the Textbox But I can input the text by using tab to reach the Textbox
Below is the function I use to open Fragment when I clicked navigation
(In my HomeActivity.kt)
private fun openFragment(fragment: Fragment) {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.container, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
Below is Xml of container
(In my Activity_Home.xml)
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:clickable="true"
android:focusable="true"/>
Below is one of Fragment
class PersonalFragment : Fragment() {
var Personal = getActivity()?.getApplicationContext();
companion object {
fun newInstance(): PersonalFragment = PersonalFragment()
}
override fun onCreateView(inflater: LayoutInflater, container:ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_personal, container, false)
}
}
Below is Xml of Fragment
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_green_light">
<Button
android:id="#+id/uploadButton"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:layout_marginStart="8dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="8dp"
android:background="#drawable/rounded_select_photo_button"
android:text="#string/UploadIMG"
android:textColor="#android:color/white"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/et_nickname"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginStart="8dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="8dp"
android:background="#drawable/rounded_etext"
android:ems="10"
android:gravity="center"
android:hint="#string/hint_nickname"
android:importantForAutofill="no"
android:inputType="textPersonName|text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/uploadButton" />
<EditText
android:id="#+id/et_birthday"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="8dp"
android:background="#drawable/rounded_etext"
android:gravity="center"
android:hint="#string/hint_birthday"
android:importantForAutofill="no"
android:inputType="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.511"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/et_nickname" />
</androidx.constraintlayout.widget.ConstraintLayout>
override onCreateView as follows:
override fun onCreateView(inflater: LayoutInflater, container:ViewGroup?, savedInstanceState: Bundle?): View? {
var view = inflater.inflate(R.layout.fragment_personal, container, false)
var button = view.findViewById(R.id.uploadButton)
button.setOnClickListener(view ->
Toast.makeText(this, "clicked", Toast.LENGTH_LONG).show()
)
return view
}
and rerun the app. by click on Button you should see clicked

Android Add fragment within a fragment

I'm trying to add a fragment within a subclass of DialogFragment. I have a FrameLayout with an id pref_container that I want to replace with a fragment called settingsFragment.
The problem is that fragmentTransaction can't find R.id.pref_container, I don't know if this is because pref_container is only inflated in onCreateView rather than being part of the activity?
I am very new to Android programming, so thank you for your time.
class QuestionnaireDialog : DialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
this.setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTheme)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.questionnaire_layout,container,true)
val settingsFragment = ExercisesOnlySettingsFragment()
val ft = fragmentManager?.beginTransaction()
/// **** Here fragmentTransaction can't find R.id.pref_container because it's inflated later and not loaded as part of the activity? ****
ft?.add(R.id.pref_container, settingsFragment)
ft?.commit()
return view
}
}
The runtime error I get is
No view found for id 0x7f09009c (com.lescadeaux.kegel:id/pref_container) for fragment ExercisesOnlySettingsFragment{4ba4b89 #1 id=0x7f09009c}
Here is relavent part of my XML for questionnaire_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"
android:id="#+id/questionnaireLayout"
android:background="#android:color/background_light">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView"
app:layout_constraintTop_toBottomOf="#+id/textView5"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<!-- RELAVENT PART -->
<FrameLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:id="#+id/pref_container">
</FrameLayout>
<!-- RELAVENT PART -->
</LinearLayout>
</HorizontalScrollView>
</android.support.constraint.ConstraintLayout>
Please try to use getChildFragmentManager() (or its kotlin equivalent) instead of getFragmentManager() you are using - it will work.

Android Navigation Component Not Displaying Fragment

I'm trying to implement the new Android Navigation Component in an existing app.
I have 2 fragments that are identical except for their name. When I set the startDestination to fragment2, the fragment seems to be shown correctly. When the startDestination is set to fragment1, I don't see the inflated view but I do see the "Fragment 1 created" toast.
What am I doing incorrectly?
class Fragment1 : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
Toast.makeText(context, "Fragment 1 created", Toast.LENGTH_LONG).show()
return inflater.inflate(R.layout.fragment1, container, false)
}
}
class Fragment2 : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
Toast.makeText(context, "Fragment 2 created", Toast.LENGTH_LONG).show()
return inflater.inflate(R.layout.fragment2, container, false)
}
}
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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/nav_graph"
app:startDestination="#id/fragment1">
<fragment
android:id="#+id/fragment1"
android:name="com.package.anotherpackage.ui.Fragment1"
android:label="fragment1"
tools:layout="#layout/fragment1"/>
<fragment
android:id="#+id/fragment2"
android:name="com.package.anotherpackage.ui.Fragment2"
android:label="fragment2"
tools:layout="#layout/fragment2"/>
</navigation>
MainActivity layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
android:background="#android:color/background_light"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="#navigation/nav_graph"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</FrameLayout>
Fragment1 Layout:
<?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.Fragment1"
>
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragment 1"
/>
</FrameLayout>
Dependencies:
//Navigation
def nav_version = "1.0.0-alpha05"
implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
implementation "android.arch.navigation:navigation-ui-ktx:$nav_version"
// optional - Test helpers
androidTestImplementation "android.arch.navigation:navigation-testing-ktx:$nav_version"
I had a layout in another library in my project with the name "fragment1.xml".
It must have been conflicting with the other layout I was trying to inflate in Fragment1. Renaming the Fragment1 layout fixed the issue.

Categories

Resources