androidup-button

Dont know why my up button is not wotking


Here i setup this on oncreate method of second avticity

        super.onCreate(savedInstanceState);
//        Actionbar
        getActionBar().setDisplayHomeAsUpEnabled(true);
        setContentView(R.layout.new_message);

And this is onOptionsItemSelected

   public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();        
        if (id == R.id.action_save) {
            Intent newMessage = new Intent(getApplicationContext(),NewMessage.class);
            startActivity(newMessage);

        }
        if(id == R.id.home){
            Toast.makeText(getApplicationContext(), "Home button click", 2000).show();
            Intent intent = new Intent(getApplicationContext(),MainActivity.class);
            startActivity(intent);
        }
/*        return super.onOptionsItemSelected(item);*/
        return true;
    }

Is there anything i need to change somewhere else like Manifest or some othere there is no code for back activity on MainActivity


Solution

  • problem:

    if(id == R.id.home)
    

    You are using the id from your R java of your project which will definitely return false it is supposed to be the native android home not your R generated id for home.

    solution:

    if(id == android.R.id.home)