The back button in the toolbar does not work

I just have an Activity that is a child of the ActionBarActivity class. In the method I installed, OnCreate supports the toolbar. To do this, I override OnOptionsItemSelected, so when I click the back button, some action was performed

The code is as follows:

[Activity (Label = "SimplyActivity", Theme="@style/MyTheme")] public class SimplyActivity : ActionBarActivity { private Toolbar toolbar; // ... OnCreate method this.toolbar = FindViewById<Toolbar>(Resource.Id.toolbar); SetSupportActionBar (this.toolbar); SupportActionBar.SetDisplayHomeAsUpEnabled (true); SupportActionBar.SetHomeButtonEnabled (true); public override bool OnOptionsItemSelected (IMenuItem item) { if (item.TitleFormatted == null) this.OnBackPressed (); return base.OnOptionsItemSelected (item); } 

Unfortunately, while the toolbar is displayed correctly, this is no longer a reaction to keystrokes. I would add that in other actions (which use fragments) everything works correctly.

Please help me

+6
source share
7 answers

The problem turned out to be really strange. In the layout using the action bar, there was a RelativeLayout. After changing the attribute LinearLayout android: gravity = "vertical", everything works correctly.

Thank you all for your help.

+1
source

It should work as follows

 public override bool OnOptionsItemSelected(IMenuItem item) { //Back button pressed -> toggle event if (item.ItemId == Android.Resource.Id.Home) this.OnBackPressed(); return base.OnOptionsItemSelected(item); } 
+8
source

Try something like this:

Just add these lines to your OnCreate method:

  SupportActionBar.SetDisplayHomeAsUpEnabled(true); 

Then override the OnOptionsItemSelected method as shown below.

 public override bool OnOptionsItemSelected(IMenuItem item) { if (item.ItemId != Android.Resource.Id.Home) return base.OnOptionsItemSelected(item); Finish(); return true; } 
+8
source

try this.toolbar.setNavigationOnClickListener and make it handle onBackPressed or popBackstack according to ur needs.

+4
source

Try to do this:

 [Activity (Label = "SimplyActivity", Theme="@style/MyTheme")] public class SimplyActivity : ActionBarActivity { private Toolbar toolbar; // ... // OnCreate method this.toolbar = FindViewById<Toolbar>(Resource.Id.toolbar); SetSupportActionBar (this.toolbar); SupportActionBar.SetDisplayHomeAsUpEnabled (true); SupportActionBar.SetHomeButtonEnabled (true); //dont forget this this.toolbar.SyncState(); this.toolbar += ClickedMenu; public override bool OnOptionsItemSelected (IMenuItem item) { this.OnOptionsItemSelected(item); return base.OnOptionsItemSelected (item); } public void ClickedMenu(object sender,SupportToolbar.MenuItemClickEventArgs e) { switch (e.Item.ItemId) { //your TitleFormatted ID case Resource.Id.action_edit: //do stuff here this.OnBackPressed (); break; } } protected override void OnPostCreate(Bundle savedInstanceState) { base.OnPostCreate(savedInstanceState); this.toolbar.SyncState(); } 
+4
source
 @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { // do something } return super.onOptionsItemSelected(item); } 
+3
source

I suggest you use this code snippet to use the custom back button on the toolbar:

First step: Add a reverse icon button to the drawing folder.

Second step: add the toolbar to your AppBarLayout as follows:

 <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/chart_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> 

Third step: in your onCreate find Views:

 Toolbar toolbar = (Toolbar) findViewById(R.id.chart_toolbar); 

4th step: add a support action bar to the toolbar:

 setSupportActionBar(toolbar); if (getSupportActionBar() != null) { getSupportActionBar().setDisplayShowHomeEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } 

Fifth step: Add the desired icon to the button:

 toolbar.setNavigationIcon(ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_chevron_left)); 

6th step: set the click listening button for the back button:

 toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NavUtils.navigateUpFromSameTask(Chart.this); } }); 

and finally override oncreateoptionsmenu and onoptionsitemselected methods:

 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.my_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { return true; } 
0
source

All Articles