How to set back not up button on Actionbar - android

I have AlbumListActivity, I am fetching list of albums from internet. When album on the list is clicked, user is taken to AlbumDetailActivity. In AlbumDetailActivity when I press phone's back button, it takes me back to AlbumListActivity and when I press back button on ActionBar it again takes me back to AlbumlistActivity but reloading data in the list. I red this post and I understand that Back and Up are different things. But in this case it really does not matter because AlbumDetailActivity will not be open by another application.

on pressing the Up button, just call the
onBackPressed()
method like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}

Related

Show an Alert on clicking Up button of ActionBar

I want to show an Alert Dialog when the user clicks on the up button of ActionBar. I have implemented onBackPressed() already. What I want to do now is trigger same action as for pressing Back button when user clicks the Up button from ActionBar.
I tried using onNavigateUp(). However, it just returns me to the parent activity without showing the Alert Dialog.
from this answer you can override onOptionsItemSelected to detect action
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
//enter your code here
return true;
}
}
return super.onOptionsItemSelected(item);
}
try it

How to properly handle intercepting a Fragment's/ Activity up navigation top back arrow( <- ) so fragment/Activity only closes based on a condition

Pls I want to show an alert dialog after the up navigation arrow(up button) is clicked. But I don't want it to go back if certain conditions are not met.
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case android.R.id.home:
if(!(editGroceryName.equals(oldGroceryName))){
showWarningAlert();
return false;
} else{return true;}
default:
return super.onOptionsItemSelected(item);
}
}
I added the alert dialog code within android.R.id.home on the onOptionsItemSelected() method but the dialog shows briefly then closes with the hosting activity then navigates back to MainActivity which was initially on back stack. Pls here is my code from SecondActivity
Pls what can I add to the code so it doesn't go back or so that only the alert dialog positive OK button can close the activity
Thanks, This fixed it for me. Returning true means you're handling things, so fragment doesn't close. While false tells the system you have no objection and that it should proceed in closing the fragment/Activity.
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case android.R.id.home:
if(!(editGroceryName.equals(oldGroceryName))){ // Name is different
showExitWarningAlert();
return true; // Handled - Fragment do not close
} else{return false;}
default: return super.onOptionsItemSelected(item);
}
}

back button is faster than calling finish() in onOptionItemSelected menu

I have a simple activity with an image view and a simple menuItem which is used for came back in the previus activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I just realized that if I press the back button for coming back in the previous activity, android do it really fast, but pressing the menu item above, it waste a second. why?
The default way of doing a return back is
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
return super.onOptionsItemSelected(item);
}
}
By calling finish() you schedule the current activity for destruction, i.e. to call onDestroy(). This does some clean-up and thus produces an overhead and you experience therewith some latency. So, if you don't really have to use finish() there (e.g. to close some dialogs or cursors), just omit it and use the provided default solution.
Hope this helps!

Lollipop activity transitions: Back button vs. back from toolbar differences?

I have an app where I'm doing activity transitions for a company directory. When a search result is selected, the animation of their photo to the detail screen works. And if I hit the back button, the reverse animation occurs. However, if I hit the back arrow from the Toolbar, the reverse animation never occurs.
The detail screen is a new DetailActivity with a single fragment in it called DetailFragment. I'm not doing anything special on "onBackPressed". Should I be?
If you want the return transition to play, you'll need to listen for the up-navigation button click and call finishAfterTransition() directly:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finishAfterTransition();
return true;
}
return super.onOptionsItemSelected(item);
}

Up Button in ActionBar with the possibility to move more activities

I'm doing an app for Android. In the DetailActivity, there is the ActionBar with the Up Button. When I press it, it would return in the previous activity. But the DetailActivity can be launched from different activity.
Instead of using NavUtils.navigateUpTo(this, upIntent) , you can just use finish() or onBackPressed(). This would finish the activity, and go back to the previous one. So handling of the Up button being pressed would look like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);

Categories

Resources