Wednesday, January 7, 2015

Up / Home Button in Android did not work?

Recently I had been developing an android app. At the final stage, I had the situation that the Up/Home button did not work.

I was sure, it was working at initial stages. Fortunately because of version control, I could track down the problem, and it turned out to be very very simple one.


Obviously I googled for the problem, and the very common suggestion was assigning android:parentActivityName in AndroidManifest.xml as explained in HERE. But this was not my problem, I had done exactly as it says.

I also had another solution. The solution was manipulating the function onOptionsItemSelected


@Override
public boolean onOptionsItemSelected(MenuItem item) { 
        switch (item.getItemId()) {
        case android.R.id.home: 
            onBackPressed();
            return true;
        }

    return super.onOptionsItemSelected(item);
}

However this seem to be rather hack. Because I know somewhere it is mentioned that: 
// 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.
And because I had mentioned the parent activity properly, I expected it to work properly.

SO WHAT WAS THE MISTAKE I DID?

Well my mistake turned out to be, I placed
return true;
in onOptionsItemSelected


So the solution was to use:


return false
OR
return super.onOptionsItemSelected(item);








No comments:

Post a Comment