简体   繁体   中英

Back button in Toolbar not working

I have simply Activity which is child of ActionBarActivity class. In the method I set OnCreate supported the toolbar. For this I override the OnOptionsItemSelected, so when I press the back button was performed some action

The code looks like 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);

            public override bool OnOptionsItemSelected (IMenuItem item)
            {
                if (item.TitleFormatted == null) this.OnBackPressed ();
                return base.OnOptionsItemSelected (item);
            }

Unfortunately, as long as the toolbar is displayed correctly, this is no longer any reaction when keys are pressed back. I would add that in other activities (which uses fragments) everything works correctly.

Please help me

It should work like this

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

    return base.OnOptionsItemSelected(item);
}

Try something like this :

Just add this lines in your OnCreate method:

  SupportActionBar.SetDisplayHomeAsUpEnabled(true);

Then override the OnOptionsItemSelected method as given below.

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

试试this.toolbar.setNavigationOnClickListener并根据您的需要使其处理onBackPressed或popBackstack。

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();     
        }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
  if(item.getItemId() == android.R.id.home) {
  // do something
  }
  return super.onOptionsItemSelected(item);
}

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

Thanks all for help

I suggest you to use this code snippet to use a custom back button in your toolbar :

First Step : Add an icon back button into drawable folder .

Second Step : add toolbar into your AppBarLayout like this :

<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 like this :

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

4th Step : add support action bar to your toolbar :

setSupportActionBar(toolbar);

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

5th Step : Add desire icon to your button :

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

6th Step : set a click listener for your 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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM