I have the following code to enable to home button to act as a back button. The problem I'm facing is from this activity if I use the real back button it simply goes back to the previous activity just as I left it. If I use the home button it's reloading the page so I lose what was previously done to it. I'm sure it's something simple I'm missing.
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.census_management_search, menu);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case android.R.id.home:
Intent intent = new Intent(this, CensusManagementActivity.class);
NavUtils.navigateUpTo(this, intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Instead of Intent and NavUtils try using onBackPressed() method.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The Home/Up button is supposed to reload the new activity. However, if you want to emulate the back button functionality, you can call finish() to return to the previous activity:
case android.R.id.home:
finish();
return true;
Related
I have a TabLayout with 3 sections. Each section contains list of items.
When I click item on second section, I start new activity like this.
Intent intent = new Intent(getContext(), SecondDetailActivity.class);
startActivity(intent);
Inside of SecondDetailActivity I have an action bar with back button.
And I handle click of back button like this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
onBackPressed();
}
}
return super.onOptionsItemSelected(item);
}
My problem is:
When I click device's back button, the selected tab of the tablayout is Second tab. But when I click on action bar navigation button, the selected tab is First tab.
How can I make action bar's behavior same as device's back button?
Try to return true while your case is android.R.id.home
like
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
onBackPressed();
return true;
}
}
return super.onOptionsItemSelected(item);
}
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.
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;
}
I am using actionBar.setDisplayHomeAsUpEnabled(true); for back navigation functionality but it is not working as properly as i need it and when i am using back button of my phone it works properly.
Is there any way to code on a button such that it work same as back navigation button of phone?
Along with
actionBar.setDisplayHomeAsUpEnabled(true);
or
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
you can try using this in Activity, too:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
So your menu item "Home"'s click will be handled using that.
I know this has been answered, but i always do this in my method when having the same back-button-like capability in another ActionBarActivity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
and ofcourse:
actionBar.setDisplayHomeAsUpEnabled(true);
You may/may not used this, its up to you :)
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;
}