how to override action bar back button action in Android? - android

I need to navigate up one more step on back pressed from the action bar back icon. It only takes me to previous back stack entry. How can I override this action so I can go one more step back.
I tried removing the fragment from being added to the backstack and that worked for me. But I cannot remove it from backstack because I need to to be there for some other purpose.

if your code has this remove it -> addToBackStack(null)

Related

Android navigation component & Bottom nav view - Hard back button goes back to "Home" with non-saved state

Context
We're migrating to use the nav component in my company, and its going ok so far.
We have a bottom navigation view with 5 tabs, and using the NavigationUI to set it up.
We have "Home" as the start destination tab for our nav graph.
Using version 2.4.2 of the navigation-* libraries.
Problem
Each tab now has its own backstack, and its state is retained, however, when:
Having the "Home" tab opened, then opening FragmentA pushed, (Now backstack of that Tab is "Home" -> "FragmentA").
Then switching to another tab, let's call it TabX.
Then clicking on the hardware back button.
Expected
As pressing back would dismiss the current tab's stack, we get back to the "Home" tab with its previous state intact? (FragmentA pushed on top of it).
What happens
We go back to the "Home" tab with only the Home fragment, FragmentA is not showing.
And the weird part is, when clicking again (reselecting) the Home tab, it now shows the previously saved state (FragmentA on top of Home).
As this is not the best UX ever, what should be done in this case? is any of those behaviours expected?
Thanks in advance!
You can check your fragments are also the same as the navigation id.
for navigation popup, you can use
findNavController().popBackStack() or
<fragment
android:id="#+id/c"
android:name="com.example.myapplication.C"
android:label="fragment_c"
tools:layout="#layout/fragment_c">
<action
android:id="#+id/action_c_to_a"
app:destination="#id/a"
app:popUpTo="#+id/a"
app:popUpToInclusive="true"/>`
Also, make sure you override the onBackPressed() method from the
host activity code as:
override fun onBackPressed() { finish() super.onBackPressed() }

Android Back Navigation proper

I have made the one Main_Activity And made many Fragment.
For example I am open the First Fragment and Second Fragment open from the first.My question is if i pressed the back Button on device the application terminate.Can possible i pressed the back Button the Screen Back second Fragment to First again pressed the screen move on Main_Activity then app terminate?
You cannot directly open fragments. They need to be inside some activity.
If you want to control the action of pressing the back button, you need to override the onBackPressed() method of the activity.
Use getFragmentManager().popBackStack() to go back to previous fragment and override onBackPress() method if required.

NullPointerException Android ActionBar Back (but not Menu Bar Back)

I get an error with the ActionBar back button when I'm moving back to a previous activity, but when I use the Menu Button Back, everything works as planned! I think I have an idea of what is going on, because using the Action Bar back button causes a recreation of the previous activity, but I don't want that to happen. How do I override the Action Bar (default) back button to perform like the Menu Bar back button so that OnDestroy() is not called?
I figured it out, I had to call finish() in my onMenuItemClicked function rather than using the default function.
This answer may be outdated though.

To add sub-menus to Menu button in android

I want to add action on clicking the Menu button on Android device. Suppose user is seeing Graph (one activity) now and he wants to switch to Charts. When user clicks the Menu button it has to show submenu say "Chart" this will be another activity. and then the user will switch to Charts.
How i can accomplish with this? can anybody guide me?
override onMenuItemClicked function and call startActivity()
to add Menu item to an activity you need to override
onCreateMenuItem function.

Android: Remove ALL FragmentTransactions from the back stack

I add a bunch of FragmentTransactions to the back stack in one Tab, and I want to clear them ALL from the back stack when the user selects a different Tab. I can't find a method to clear stuff off the back stack, only methods to pop them off, and these don't even return the Fragment to you so you can close them. Any ideas?
If you add many Fragments to the backstack and want to remove them all you can do this: popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
Just remove them from the backstack using the popBackStack() methods.
Each fragment you pop is exactly the same as if the user had pressed the BACK button, and you don't do any special cleanup when the BACK button is pressed, right?

Categories

Resources