NullPointerException Android ActionBar Back (but not Menu Bar Back) - android

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.

Related

how to override action bar back button action in 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)

How to make a backpressed action for navigating to the previous fragment in activity that implement bottom navbar?

I wanna make an application that has multiple screens (screen1, screen2, and screen3), and for navigating between screens I wanna use bottom navigation bar. I wanna make it like Instagram app that can navigate back to the previous screen when the back button is pressed. I have made an app with navbar but when I press the back button the application is closed directly even after I navigate to other screen.
Override the onBackPressed() method of your activity, and write back button logic on it.

Android ActoinBar homeAsUp only works once

I'm working on an app that only uses a single Activity and switches out fragments as they are needed with the navigation drawer.
Now we want to navigate back from one of these fragments by using the homeAsUp button in the ActionBar.
I have followed all the steps to set the button up. From disabling the navigation drawer setDrawerIndicatorEnabled(false) and calling setDisplayHomeAsUpEnabled(true) in the fragment's onCreateView().
I also set setHomeButtonEnabled(true) in the MainActivity's onCreate() however because the app is already in the MainActivity, we cannot specify a Parent Activity.
Whenever I run a fresh install of the app, the homeAsUp button works and is registered in the onBackPressed(), not onOptionsItemSelected() method. However, when I close the app and run it again the button does not even register clicks.
In onBackPressed() I check a few conditions, but it does not block the button press.
In onOptionsItemSelected() I check for android.R.id.home.
Unfortunately, I cannot post the code.
This post describes what I'm trying to achieve: Switching between Android Navigation Drawer image and Up caret when using fragments
I managed to fix the issue I was experiencing. It was a very simple mistake.
Because I'm not the original author of this code, I went through the MainActivity thoroughly.
As it turns out the original author called setDisplayHomeAsUpEnabled in the onCreate function (which is extremely long), but near the end he also called setSupportActionBar, which made the first call of setDisplayHomeAsUpEnabled useless. Moving setDisplayHomeAsUpEnabled below setSupportActionBar fixed my problem.
If you did everything correctly, make sure that your code is written in the correct order.
Furthermore, if you use custom toolbars in other fragments, remember to set your original Support Action Bar by calling setSupportActionBar(toolbar.find(this)) in onResume of your MainActivity.

Disable home and back buttons in android

I'm doing an appication under phonegap (more specific in android). How can I disable the home, menu and back buttons in the screen, the idea is when i press any of the buttons, show a verification dialog, if the answer is correct, close the app. When i assign the function button doesn't show the dialog
For the home menu remove the onCreateOptionsMenu() function and also you wouldn't need the onOptionsItemSelected() function, so to hide the menu remove these two.
For the back buttons there are two of them:
The hard button in device that navigates in the app until it hits the main activity then pops out of the app, can be controlled by overriding this method onBackPressed()
And the one in the action bar that navigates all inside the app itself but never pops the user out of the app, can be set by setDisplayHomeAsUpEnabled() which takes a Boolean true to make it working, false to disable it.

Force Contextual Action Bar (CAB) for an Activity

I want an acitivy to show a Contextual Action Bar, not normal ActionBar. The reason is that I show this activity in resonse for user long-clicking an item.
I know how to hide title and in general make the normal ActionBar look similar to CAB, but not quite. May be there is a simple way of doing it?
Update:
My question is similar, but not a complete duplication of how to invoke the actionbars context menu like behavior question
The difference is that I dont want at all to show a "normal" ActionBar, always the one that looks like Contextual ActionBar.
The suggestion to move to ActionMode right in the activity onCreate.
ActionMode.Callback onDestroyActionMode() that is called when ActionMode ends, we would simply finish() the activity.
It is a good one and almost does it. There are two subtle problems with this approach.
One, there is an observable flicker when ActionBar shows and immediately goes to CAB.
Second, the more substatial one is that there is no way to differentiate between edits that we want to "accept" and "decline". I would want checkmark mean that edits need to be accepted, and "back" button mean that edits need to be discarded. Unfortunately the suggested hack results in both routes ending with the call to onDestroyActionMode()
I could have added more CAB action items, for saving or exiting without saving, but this defeats the purpose of having separate editing activity.

Categories

Resources