androidback-buttonandroid-2.3-gingerbread

android - up indicator not working properly


I've built this to test how my app works on lower end devices (having small amount of resources, older version of android etc), this case up Indicator is not working only in my 2.3.5 api 10. Not working means it doesn't take me back to the parent activity. However a Toast is made to see what's going on, this time only the toast is shown perfectly on that device but still not navigating to the parent activity (main activity)

Main2Activity :

public class Main2Activity extends AppCompatActivity {

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


        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                Toast.makeText(this, "Up Button is clicked!", Toast.LENGTH_SHORT).show();
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}

parent was set in Mainefest :

....
    <activity
                android:name=".Main2Activity"
                android:parentActivityName=".MainActivity"></activity>
        </application>
....

Again, this is working perfectly without any issue in emulators and my other devices I tested like Nexus 5(API 19), not working only:

enter image description here

Toast is created :

enter image description here

Any idea why up indicator is not taking me back to the main activity?


Solution

  • In your onOptionsItemSelected() add NavUtils.navigateUpFromSameTask(this);

    Also return true to consume the event..

    From Documentaiton

    void navigateUpFromSameTask (Activity sourceActivity) : Convenience method that is equivalent to calling navigateUpTo(sourceActivity, getParentActivityIntent (sourceActivity)). sourceActivity will be finished by this call.

      @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            Toast.makeText(this, "Up Button is clicked!", Toast.LENGTH_SHORT).show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    

    Also To support API levels 4 - 16, you can also declare the parent activity with a <meta-data> element that specifies a value for "android.support.PARENT_ACTIVITY". For example:

    <activity
        android:name="com.example.app.ChildActivity"
        android:label="@string/title_child_activity"
        android:parentActivityName="com.example.app.MainActivity" >
        <!-- Parent activity meta-data to support API level 4+ -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.app.MainActivity" />
    </activity>