Add back button to action bar - android

I have been trying to add a back button to the action bar.
I want my view to look like this:
I want to add the back button in the left of the action bar.
I added this code
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
but it doesn't work.
How can I fix this?

After setting
actionBar.setHomeButtonEnabled(true);
Add the following code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Add
actionBar.setHomeButtonEnabled(true);
and then add the following
#Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
switch (menuItem.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(menuItem);
}
}
As suggested by naXa I've added a check on the itemId, to have it work correctly in case there are multiple buttons on the action bar.

this one worked for me:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your_activity);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// ... other stuff
}
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
The method onSupportNavigateUp() is called when you use the back button in the SupportActionBar.

After setting
actionBar.setHomeButtonEnabled(true);
You have to configure the parent activity in your AndroidManifest.xml
<activity
android:name="com.example.MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat" />
<activity
android:name="com.example.SecondActivity"
android:theme="#style/Theme.AppCompat" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.MainActivity" />
</activity>
Look here for more information http://developer.android.com/training/implementing-navigation/ancestral.html

There are two ways to approach this.
Option 1: Update the Android Manifest
If the settings Activity is always called from the same activity, you can make the relationship in the Android Manifest. Android will automagically show the 'back' button in the ActionBar
<activity
android:name=".SettingsActivity"
android:label="Setting Activity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.example.MainActivity" />
</activity>
Option 2: Change a setting for the ActionBar
If you don't know which Activity will call the Settings Activity, you can create it like this. First in your activity that extends ActionBarActivity (Make sure your #imports match the level of compatibility you are looking for).
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings_test);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
Then, detect the 'back button' press and tell Android to close the currently open Activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
That should do it!

Firstly Use this:
ActionBar bar = getSupportActionBar();
bar.setDisplayHomeAsUpEnabled(true);
Then set operation of button click in onOptionsItemSelected method
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

You'll need to check menuItem.getItemId() against android.R.id.home in the onOptionsItemSelected method
Duplicate of Android Sherlock ActionBar Up button

Simpler and better:
For API >= 16
Simply add "parentActivityName" for each activity in Manifest. The back button will automatically take u to the parent activity.
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >

Use this to show back button and move to previous activity,
final ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.back_dark);
#Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

if anyone else need the solution
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}

Add this line in onCreate() method
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
then Override this method
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}

Related

Navigate back from settings activity

I'm playing around with Android Studio so I created a SettingsActivity using the wizard and I'm faced with the problem that it is not possible to navigate from this settings activity back to the main activity using the "up" arrow in the Actionbar.
The setup of the Actionbar looks like this:
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
Actionbar is not null btw.
And the parentActitvityName is set in the AndroidManifest:
<activity
android:name=".SettingsActivity"
android:label="#string/title_activity_settings"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.demo.app.MainActivity" />
</activity>
However, a click on the arrow does nothing. Not even onOptionsItemSelected gets triggered.
Seems like this is exactly the same problem
Action bar setDisplayHomeAsUpEnabled not working on ICS but navigating back from a detail to an overview activity is working fine in the very same app. Moreover I set MinSDK to 15 and TargetSDK to 23.
override the onOptionsItemSelected method on your AppCompatPrefernceActivity and make it 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:
super.onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Vspallas answer is correct. The mistake was on my side. I had an onOptionsItemSelected method inside the preferenceFragment, not in the Activity. Mea culpa.
You can use this inside the settings activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Then in androidManifest is the same as that you have did
<activity
android:name=".SettingsActivity"
android:parentActivityName=".MainActivity"
android:label="#string/title_activity_settings">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app_name.MainActivity" />
</activity>
this com.example.app_name is you App package name

Dynamic parent activity for Android up \ back navigation in Preferences menu

I've a Preference activity for a options menu in my Android app.
I've enabled the Up \ Back navigation on the ActionBar and I need to come back to the previous activity that called the Options menu.
For the Preference activity, I could use in the manifest:
android:parentActivityName="mypackage.com.MainActivity"
but how come back to other activies ? The Options menu is called from 4 different activities.
public class Prefs extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
ActionBar actionBar = getActionBar();
// Enabling Up / Back navigation
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
Make sure that you have declared the parent activity in the manifest like this...
<activity
android:name="com.myapp.SetPreferenceActivity"
android:parentActivityName="com.myapp.MainActivity"
>
and then make sure to add the case into your onOptionsItemSelected method...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
//Take me back to the main activity
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
if you prefer to have the up button point to a custom activity, you can just use an intent.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
Intent changeActivity = new Intent(this,OtherActivity.class);
startActivity(changeActivity);
return true;
}
return super.onOptionsItemSelected(item);
}
The default behaviour of the Back button is that it will get you back to the calling activity. The system maintains a back stack of activities while the user navigates the application. Do you need to override this functionality? Please be more specific.
Solved in this way:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
}
return true;
}

Up Navigation from Actionbar in Android

In my android app i am using up navigation icon on actionbar.
In my child activity i have set
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
In manifest I have set
<activity
android:name="app.sclms.UserAccount"
android:label="#string/app_name"
android:parentActivityName="app.sclms.MainMenu">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="app.sclms.MainMenu" />
</activity>
But whenever I click on up icon my application get exited.
What is missing here, I don't understand.
Please read this section: http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp
Specifically, add this code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
I however do not recommend using navigateUpFromSameTask as it will cause your ParentActivity to be re-created instead of being resumed on ICS and lower. The reason for this behavior is because NavUtils behaves differently for Pre JellyBean and Post JellyBean as explained in this SO.
A better way is to do this is to handle the Up action manually in the Child Activity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
startActivity(new Intent(this, ParentActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP));
default:
return super.onOptionsItemSelected(item);
}
}
And best part of this manual approach is that it works as expected(resumes the ParentActivity instead of ReCreating it) for all API Levels.

Android action bar 'up' items, with activities that have more than one parent

I have an Android app that uses the action bar, and the flow through the system is determined in the manifest as so:
<... android:parentActivityName="com.polymorph.amsmobile.xyzActivity" >
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value="com.polymorph.amsmobile.xyzActivity" />
However there is a screen that users can go to from several points within the hierarchy and as such there is more than one 'parent' activity, so I want to dynamically specify the parent activity rather than specifying in the manifest.
My goal is to ensure that when they use the 'up' on the action bar, it will take them to the correct page of the app.
Do you mean something like this in an Activity from where you want to be able to go Up to another activitiy from the ActionBar?
#Override
protected void onCreate(Bundle b) {
ActionBar ab = getActionBar();
ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, GoToThisActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
}
If you just want to go to the previous activity, you can just do:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
break;
}

Action Bar's onClick listener for the Home button

How can I implement a custom onClickListener for the Home button of the Action Bar?
I already did a getSupportActionBar().setDisplayHomeAsUpEnabled(true); and now I want to redirect the user to a certain activity in case the Home button is clicked.
I tried with:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Intent i = new Intent();
i.setClass(BestemmingActivity.this, StartActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
return true;
}
});
default:
return super.onOptionsItemSelected(item);
}
}
but it never enters in the onMenuItemClick.
Basically, it's done just like in this link but still it doesn't enter in the listener.
if anyone else need the solution
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed(); return true;
}
return super.onOptionsItemSelected(item);
}
I use the actionBarSherlock,
after we set supportActionBar.setHomeButtonEnabled(true);
we can override the onMenuItemSelected method:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case android.R.id.home:
toggle();
// Toast.makeText(this, "home pressed", Toast.LENGTH_LONG).show();
break;
}
return true;
}
if we use the system given action bar following code works fine
getActionBar().setHomeButtonEnabled(true);
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case android.R.id.home:
//do your action here.
break;
}
return true;
}
Fixed: no need to use a setOnMenuItemClickListener.
Just pressing the button, it creates and launches the activity through the intent.
Thanks a lot everybody for your help!
answers in half part of what is happening. if onOptionsItemSelected not control homeAsUp button when parent activity sets in manifest.xml system goes to parent activity. use like this in activity tag:
<activity ... >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.activities.MainActivity" />
</activity>
You need to explicitly enable the home action if running on ICS. From the docs:
Note: If you're using the icon to navigate to the home activity, beware that
beginning with Android 4.0 (API level 14), you must
explicitly enable the icon as an action item by calling
setHomeButtonEnabled(true) (in previous versions, the icon was enabled
as an action item by default).
Best way to customize Action bar onClickListener is onSupportNavigateUp()
This code will be helpful link for helping code
you should to delete your the Override onOptionsItemSelected and replate your onCreateOptionsMenu with this code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_action_bar_finish_order_stop, menu);
menu.getItem(0).setOnMenuItemClickListener(new FinishOrderStopListener(this, getApplication(), selectedChild));
return true;
}

Categories

Resources