toolbar back arrow doesn't work - android

I create other activity and hope use toolbar back arrow to return main activity, but under code doesn't work, please help me.
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.setTitle(R.string.setting);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
System.out.println("??");
finish();
return true;
}
return true;
}
});
}

Try This Method:
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
return true;
}
return (super.onOptionsItemSelected(menuItem));
}

The Toolbar has a method for this, setNavigationOnClickListener. It allows you to listen for click events on the back arrow. Here is the documentation if you want to read more about the Toolbar.
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("??");
finish();
}
});

You should use this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}

Related

OnBackBressed not called

I have activity ActivityProfile, have getSupportActionBar().setDisplayHomeAsUpEnabled(true); implemented, onBackPressed() as well, search all over the internet and still no help.
#Override
public void onBackPressed() {
Toast.makeText(this, "OnBackpressed fired", Toast.LENGTH_SHORT).show();
super.onBackPressed();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//replaces the default 'Back' button action
if(keyCode== KeyEvent.KEYCODE_BACK) {
// something here
onBackPressed();
}
return true;
}
using the device's back button works, but not on the app...
To implement Up navigation, declare a parent of that particular activity in manifest and setDisplayHomeAsUpEnabled as true.
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
Read More https://developer.android.com/training/implementing-navigation/ancestral.html
you should use onOptionItemSelected to handle click on the bottom on action bar :
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Try this
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
This should work
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
hi add this code in your activity. when you press back button in toolbar following code execute.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_view);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
finish();
break;
default:
return super.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}

How to make home button act as back button?

I have two activities.One of them has Fragment.
From this fragment I can go to another activity, but by clicking the button "home" it goes to previous activity instead of going to the fragment of the previous activity.
Image
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_closet);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Refere below code :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You need to override below method :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()== android.R.id.home) {
finish();
return true;
}
return false;
}
getSupportActionBar().setNavigationOnClickListener(new OnClickListener {
public void onClick(View v){
onBackPressed()
}
})
Since you want same behavior as of back button you should use onBackPressed();
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Activity is not closed on clicking back button of Toolbar

Refer Below Image,
I want to go back to the previous activity.
But on clicking the back button on toolbar,Nothing is happening.
I have used the following code to achieve that still no luck.
public boolean onOptionsItemSelected(MenuItem item){
if(item.getItemId() == R.id.home)
{
finish();
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
//NavUtils.navigateUpFromSameTask(this);
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
In your OnCreate method
toolbar.setTitle(R.string.title_activity_setting);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
In your onOptionsItemSelected method
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I will work.
Add onBackPressed() method on your Activity. And super this. And when click back button call to this.onBackPressed().
Update code for this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
super.onBackPressed();
}

Can not open navigation drawer on action bar item click

i want the right drawer to be opened on menu click
i have used the following code but its not working for some reason
mDrawerToggle = new ActionBarDrawerToggle(getActivity(),mDrawerLayout,toolbar, R.string.drawer_open,
R.string.drawer_close) {
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == R.id.more) {
if (mDrawerLayout.isDrawerOpen(Gravity.END)) {
saveToPreferences(getActivity(),KEY_USER_LEARNED_DRAWER,mUserLearnedDrawer+"");
mDrawerLayout.closeDrawer(Gravity.END);
} else {
mDrawerLayout.openDrawer(Gravity.END);
}
}
return false;
}
};
please help me out
SOLVED IT BY MODIFYING THE MAINACTIVITY
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case R.id.more:
if (notificationDrawerFragment.mDrawerLayout.isDrawerOpen(Gravity.END)){
notificationDrawerFragment.mDrawerLayout.closeDrawer(Gravity.END);
}
else
{ notificationDrawerFragment.mDrawerLayout.openDrawer(Gravity.END);
}
default:
return super.onOptionsItemSelected(item);
}
}
Solved the following by modifying the menu option within the main activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case R.id.more:
notificationDrawerFragment.mDrawerLayout.openDrawer(Gravity.END);
default:
return super.onOptionsItemSelected(item);
}
}
Do you have this override:
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
also you should have overrides under ActionBarDrawerToggle:
public void onDrawerOpened(View drawerView) and
public void onDrawerClosed(View drawerView)
Try this, I had learnt this from SO:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == android.R.id.more) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
saveToPreferences(getActivity(),KEY_USER_LEARNED_DRAWER,mUserLearnedDrawer+" ");
mDrawerLayout.closeDrawer(Gravity.RIGHT);
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
}
return false;
}
};
Hope this helps.

why it don't work when I click back arrow icon?

I set the following code in my activity:
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
Why it can't work, when i click back arrow icon?
You need implement it by yourself:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}

Categories

Resources