Sorry for my english :)
I have an item in the toolbar and when the user clicks on the item I would like it to go back to previous activity or go to any other activities that I choose.
Thanks for help
actually this is my code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.back)
{
Intent i = new Intent(listActivity.this,MainActivity.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
Set your toolbar home button property,
toolbar.setDisplayHomeAsUpEnabled(true);
Try calling finish() method.
if(id==android.R.id.home)
{
this.finish();
return true;
}
As #Mahfa already said, calling finish() will always navigate you back where you came from, in your case, MainActivity.
In fact all of this answers really helped me in someways
but the exact code that worked for me is like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Related
I have A activity that consists a list of countries.
When I clicked on one country it open B activity that consists a list of images. and when I clicked on one of them it open activity C.
A(OnClick)-->OpenB-->B(OnClick)-->OpenC
and when i press back arrow on app bar it should back to B activity
Anyone has any idea how do I implement this?
Please help me
simply add below line in your onCreate()
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true)
and then add this lines
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
hope this will help you!
In your backButton you just call finish();
Example
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) // Press Back Icon
{
finish();
}
return super.onOptionsItemSelected(item);
}
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 :)
I would like to provide back navigation while clicking on the Home button of Actionbar.
In the Host Fragment
((MainActivity)this.getActivity()).fragment_Actualités=new Fragment_Actualités();
fragmentTransaction.replace(R.id.content_frame,((MainActivity)this.getActivity()).fragment_Actualités);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
((MainActivity)this.getActivity()).fragmentManager.executePendingTransactions();
((MainActivity)this.getActivity()).supportInvalidateOptionsMenu();
In the Host Activity
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
int id=0;
if(id==item.getItemId()){
if(id== R.id.home)
{
this.onBackPressed();
return true;}
}
The super.OnBackPressed works fine when I click on the back button it goes back as expected
How can I make it worked ?
Try using
fragmentManager.popBackStackImmediate()
Try changing:
this.onBackPressed();
to:
onBackPressed();
and extends FragmentActivity if you don't do it already
Reading your code again I think found the issue, you are assigning 0 to id so when you compere with item.getItemId() always return false. Try with this code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == android.R.id.home) {
this.onBackPressed();
return true;
}
}
I finally resolved it,
the matter was that, I had 2 MenuItems, the Settings and the Home One's .
So if only write R.id.Home it won't work but when changed to android.R.id.Home it works !!!
with the same code
I am seeing the up caret but nothing happens when I click it. Where do i tell it to goto home and set which activity is Home?
You need the line:
getActionBar().setDisplayHomeAsUpEnabled(true);
put that in onCreate();
That will make it clickable. The you will need to handle the click event. This is done by overriding onOptionsItemSelected()
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
default:
return super.onOptionsItemSelected(item);
}
}
If you want to use NavUtils, (which I have seen used by the ADT plugin when it makes Activities) you can replace change the implementation like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Don't forget to
import android.support.v4.app.NavUtils;
Also, you can add this to your Activity node:
<activity
android:name=".SomeActivity"
android:label="#string/activity_label" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mypackage.MainActivity" />
</activity>
NavUtils is usually the better approach. For more information, see the official Android Guidelines.
In your onCreate you have:
getActionBar().setDisplayHomeAsUpEnabled(true);
And then use:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Updated to address comments below, which correctly point out my original answer was not correct for all cases. The "home/up" button have are supposed to take you the Home Activity or the Parent Activity, respectively. From the ActionBar documents --
By default, your application icon appears in the action bar on the
left side. If you'd like, you can enable the icon to behave as an
action item. In response to user action on the icon, your application
should do one of two things:
Go to the application "home" activity, or
Navigate "up" the application's structural hierarchy
Implementing "Home" is straightforward. Launch an intent to the home activity (usually your main launch activity) of your app, clearing the backstack to the existing instance if one exists--
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
"up" is similar, except you should use
getActionBar().setDisplayHomeAsUpEnabled(true);
in your onCreate to get the icon to show as "up" instead of "home", and launch an instance of the parent activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent parentActivityIntent = new Intent(this, MyParentActivity.class);
parentActivityIntent.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(parentActivityIntent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
In both case, for Activities that can be launched from outside your application, you should add FLAG_ACTIVITY_NEW_TASK to launch into a new task instead of the callers task.
"Up" can get more complicated if your Activity can be launched from another app, and want to build a back stack for your application to get back to the root. See for this guide for more info.
My original answer, shown below, is considered bad form in the guide because it it treats the home/up button as 'back'. It will take you to the activity that invoked this activity, which is not necessarily the home or parent. It works well as 'up' for apps that have strict tree hierarchy of activities because an given activity was always launched by its parent.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
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;
}