-1

I have an issue with back button presses always exiting the app.

This is a single activity app where the initial loaded screen is a two tab viewpager2 layout with 2 seperate nav graphs, one for each tab.
The viewpager calls two "blank" fragments depending on the position (as the viewpager createFragment() needs a fragment returned - these "blank" fragments each hold one NavHostFragment i.e. android:name="androidx.navigation.fragment.NavHostFragment".

The "main" nav graph has app:defaultNavHost="true" set, the other had app:defaultNavHost="false" to be 100% explicit.
Ive tried setting both to true (no effect on back button) and one true and one not entered at all with no effect. I have all actions setup to flow between destinations in both nav graphs and forward navigation works fine. I only have logical "forward" actions added, im aiming to use "popUpTo" and "popUpToInclusive" later as necessary.

I dont have onBackPressed() called anywhere in my code (ive searched all project files). my test device is API26 and developer options doesn't have any facility to program the back button (as im going mad looking for the bug and thought id check there).

Does Viewpager2 interfere with the navigation components backstack - im trying to find this out myself without much success.

Edit

I googled a lot more and found a nearly identical issue was already resolved - In short, adding a viewpager as the root navigation with two nested nav graphs interferes with the fragment back stack and requires the viewpager adapter to set the primary navigation fragment each time the fragment is changed. Note the library is still in alpha at time of writing.

Please take this post below as how im coding the answer to my own question - once i have the code finalised ill post it as an answer (if allowed).

How to navigate back from fragments inside NavHostFragment of ViewPager2?

tommytucker7182
  • 213
  • 1
  • 5
  • 11

1 Answers1

0

If you dont't have onBackPressed then call this method in your activity and just check current activty has finish method while calling current activity from previous activity.

If nothing works then call the onBackPressed method in current activity and implement doble tap to exit in onBackPressed Method.

Checkout this post for double tap to exit : Clicking the back button twice to exit an activity

thanks & hope It helps you.

UncagedMist
  • 111
  • 1
  • 4
  • Hey mate, thanks for taking the time to answer. This didnt solve my problem, i dont currently want to implement "double tap to exit". Thanks again – tommytucker7182 May 17 '21 at 08:47