Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actionbar back button not working in android

I am creating a android app where i need to put back navigation ,so that when user click on action bar's home button user should be moved to one screen back on my app.But action bar home button is not responding to click events. Here is my code.

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }
            @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);

        ActionBar actionBar = getActivity().getActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);


        return rootView;
    } }

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.home:
        Toast.makeText(getApplication(), "Back", Toast.LENGTH_LONG).show();

        break;

    default:
        break;
    }
    return super.onOptionsItemSelected(item);
}
like image 489
Jigar Shekh Avatar asked Apr 01 '14 06:04

Jigar Shekh


People also ask

Why does my back button not work on Android?

Force Restart the Device It fixes all the glitches which can possibly lead to issues like these. For this, hold down your smartphone's power button for 7-10 seconds. After the phone restarts, the home and the back button on your Android phone should work just fine.


1 Answers

case android.R.id.home:

try to use this instead of case R.id.home:

like image 104
Jay Vyas Avatar answered Oct 02 '22 23:10

Jay Vyas