Show an Alert on clicking Up button of ActionBar - android

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

Related

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);
}
}

How to set back not up button on Actionbar

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);
}

Back button as Menu in fragment

I want to add a back button as menu in the left of the action bar in fragment. But I don't want the back arrow as my icon.
actionBar.setDisplayHomeAsUpEnabled(true);
The above code line gives the back arrow symbol. Instead i want to use some custom image. Also using that custom image should get it back to its previous activity.
I added something like:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
It worked for me.
Add this to your theme in styles.xml:
<item name="android:homeAsUpIndicator">#drawable/my_icon</item>
It will override the default icon. By default it uses the following id:
android.R.id.home
You can use this id to navigate back to your previous activity:
View homeView = getActionBar().getCustomView().findViewById(android.R.id.home);
homeView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v)
{
//Go back
}
});
or
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
//Return back to your activity!
return true;
}
return super.onOptionsItemSelected(item);
}
For more information check the official documentation at : https://developer.android.com/training/implementing-navigation/ancestral.html

Home Back button in Navigation bar doesn't work in my android app

After activating Navigation Up Button in Action Bar, Home Back button in Navigation bar doesn't work in my app. Where am I doing wrong? when I click on the navigation app up button, it works. But, Navigation back button as you can see on the pivture doesn't work...
Back Button
enter image description here
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.action_email:
emailMenuItem();
break;
case R.id.action_settings:
settingsMenuItem();
break;
}
return true;
}
First understand following things:
#Override
public void onBackPressed() {
super.onBackPressed();
// executed this when hardware back button is pressed
}
is called when you press back button.
AND
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case android.R.id.home:
// executed this when back button on Actionbar is pressed
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
is called when you press back button on Actionbar.
Comment below if you have any queries.

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