Search code examples
androidandroid-toolbar

Back button of Toolbar doesn't work


The detailed activity of my app looks like this.

detail activity

When I add the menu items, the back button of the toolbar doesn’t work. If I take them out, everything works fine. Here is my code for that specific activity.

public class DetailActivity extends AppCompatActivity {

ImageView mImageView;
TextView mTitle;
TextView mDate;
TextView mDescription;

private String newsTitle;
private String newsImage;
private String newsDate;
private String newsDescription;
private static String NEWS_SHARE_HASHTAG ="#PopularMoviesApp";
private String date1;
private String date2;
private String newsUrl;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);

    Toolbar toolbar = (Toolbar)findViewById(R.id.detail_toolbar);
    setSupportActionBar(toolbar);

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

    Intent i = getIntent();

    mImageView = (ImageView)findViewById(R.id.detail_image_view);
    mTitle = (TextView)findViewById(R.id.detail_title);
    mDate = (TextView)findViewById(R.id.detail_publish_date);
    mDescription = (TextView)findViewById(R.id.detail_description);

    newsImage = i.getStringExtra("image");
    newsTitle = i.getStringExtra("title");
    newsDate = i.getStringExtra("date");
    newsDescription = i.getStringExtra("description");
    newsUrl = i.getStringExtra("url");

    date1 = newsDate.substring(0,10);
    date2 = newsDate.substring(11,19);

    Picasso.with(this).load(newsImage)
            .placeholder(R.drawable.ic_broken_image)
            .into(mImageView);

    mTitle.setText(newsTitle);
    mDescription.setText(newsDescription);
    mDate.setText(date2 + ", " + date1);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.detail_menu,menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);

    if(item.getItemId() == R.id.detail_browser_btn){
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(newsUrl));
        startActivity(browserIntent);
    }else if(item.getItemId() == R.id.detail_share_btn){
        Intent shareIntent = createShareNewsIntent();
        startActivity(shareIntent);
    }

    return true;
}

private Intent createShareNewsIntent() {
    Intent shareIntent = ShareCompat.IntentBuilder.from(this)
            .setType("text/plain")
            .setText(mTitle + NEWS_SHARE_HASHTAG + "\n\n\n" + newsTitle
                    + "\n\n\n" + newsDescription
                    + "\n\n\n" + newsDate)
            .getIntent();

    return shareIntent;
}
}

How can this problem be fixed?


Solution

  • Add

    if(item.getItemId() ==android.R.id.home){
      onBackPressed();
    }
    

    to your onOptionsItemSelected override.

    This way, when you're tapping that button you'll invoke the onBackPressed method, which is the default action to go back (a cleaner solution than brutally invoking finish())

    That should do it.