简体   繁体   中英

Android Studio run command when back button is clicked in Activity

This back button are added automatically on ActionBar

在此处输入图片说明

I want button to run the command below instead.

if (mWebView.canGoBack()) 
{
    mWebView.goBack();    
}
else
{
    super.onBackPressed();
}

The button is automatically added I didn't use any command I don't know where to place my command can anyone help me?

Try this,

    toolbar.setNavigationOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            if (mWebView.canGoBack()) 
            {
                mWebView.goBack();    
            }
            else
            {
                super.onBackPressed();
            }
        }
    });

try this,

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // todo: goto back activity from here

                if (mWebView.canGoBack()) 
                {
                    mWebView.goBack();    
                }
                else
                {
                    super.onBackPressed();
                }
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

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