Search code examples
javaandroidandroid-activitylistactivity

How to give back button to toolbar in ListActivity


I need to give back button to toolbar which extends ListActivity

I have been using following codes to add back button to toolbar

// Adding Toolbar to Main screen
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_home);
    // Adding Toolbar to Main screen
    toolbar.setTitle("Please Confirm Your Action");
    toolbar.setTitleTextColor(Color.WHITE);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

But all those activity extends AppCompatActivity.But now I need to give back button in ListActivity and it returns error in setSupportActionBar(toolbar)

Please help


Solution

  • What I done is i have included a back button using an image like this

    toolbar.setNavigationIcon(R.drawable.ic_action_back);
    

    Then I add an setNavigationOnClickListener on the toolbar and finished the activity

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });