When toolbar back button is pressed

后端 未结 4 1472
梦谈多话
梦谈多话 2021-01-28 00:01

how can I create a condition on my android app where When I pressed the back button on the toolbar some code will happening.

I tried this but it doesnt work.

<         


        
相关标签:
4条回答
  • 2021-01-28 00:36
    Toolbar toolbar;
    toolbar.setNavigationIcon(R.drawable.left_arrow_thick);
    setSupportActionBar(mToolbar);
    toolbar.setNavigationOnClickListener(new View.OnClickListener()
    {
         @Override
         public void onClick(View v) 
         {
                    //write your code here
                    stopTimerTask();
         }
    });
    
    0 讨论(0)
  • 2021-01-28 00:39

    Toolbar manipulations.

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            // Title and subtitle
            toolbar.setTitle(R.string.about_toolbar_title);
            toolbar.setSubtitleTextColor(Color.WHITE);
            toolbar.setTitleTextColor(Color.WHITE);
            toolbar.setBackgroundColor(getResources().getColor(
                    R.color.themeToolbarColor));
            toolbar.setNavigationIcon(R.drawable.ic_action_back);
            toolbar.setNavigationOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
    
    0 讨论(0)
  • 2021-01-28 00:42

    Just used below code:-

    toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_nav_back));
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("cek", "home selected");
    //do something here
        }
    });
    
    0 讨论(0)
  • 2021-01-28 00:43

    try this :

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // this takes the user 'back', as if they pressed the left-facing    
    
          triangle icon on the main android toolbar.
            // if this doesn't work as desired, another possibility is to call   
    
            `finish()` here.
            getActivity().onBackPressed();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
    }
    
    0 讨论(0)
提交回复
热议问题