user1418717
user1418717

Reputation: 425

Android - How does up button work?

From Android documentation:

...Android 4.1 (API level 16) or higher...performing Up navigation simply requires that you declare the parent activity in the manifest file and enable the Up button for the action bar.

This works fine but, when I commented meta entry for parent from manifest and getSupportActionBar().setDisplayHomeAsUpEnabled(true); from activity class, I can still see up button, both in real device and emulator.

uses-sdk: minSdkVersion="11" and targetSdkVersion="19"

BTW, I had uninstalled app from emulator, did a clean build and tested again in both devices but, I can still see up button without going as per documentation.

Please help me understand - how does up button work even when not doing as per documentation?

Thanks!

EDIT: MANIFEST and Activity code.

MANIFEST:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo.Light">
        <!-- 
        android:theme="@style/AppTheme" >
         -->
        <activity
            android:name="com.example.myfirstapp.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.myfirstapp.DisplayMessageActivity"
            android:label="@string/title_activity_display_message"
            android:parentActivityName="com.example.myfirstapp.MainActivity" >
            <!-- 
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myfirstapp.MainActivity" />
                 -->
        </activity>
    </application>

</manifest>

ACTIVITY CLASS:

public class DisplayMessageActivity extends Activity {

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message + " chil" );

        setContentView(textView);

        /*
        // Make sure we're running on Honeycomb or higher to use ActionBar APIs
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
        */
    }

    /*
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    /*
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }
*/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.display_message, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

SNAPSHOT: enter image description here

Upvotes: 1

Views: 582

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

Just tested on emulator

Yes it works. You still have

android:parentActivityName="com.example.myfirstapp.MainActivity".

Parent activity meta-data to support 4.0 and lower. So if you run on above 4.0 it works.

You can cross check my commenting or removing the above.

Upvotes: 1

Related Questions