简体   繁体   中英

going back to the parent activity on pressing app icon button

hi there im a android beginner, i just finish the android actionbar tutorial section here . my problem is on Add Up Button for Low-level Activities subject. i done it but the only problem i have is i don't know why it doesn't come back to main activity when i press Up button. it just stay in child activity when i run app: this is my MainActivity code:

package com.example.action;

import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity  {

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

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_search:
                openSearch();
                return true;
            case R.id.action_settings:
                openSettings();
                return true;
            case R.id.home:
            {
                NavUtils.navigateUpFromSameTask(this);
                return true;
            }
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    public void openSearch()
    {
        TextView textv=new TextView(this);
        textv.setText("Search");
        textv.setTextSize(50);
        setContentView(textv);
    }
    public void openSettings()
    {
        TextView textv=new TextView(this);
        textv.setText("Search");
        textv.setTextSize(50);
        setContentView(textv);
    }


}

AndroidManifest.xml:

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

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="18" />

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

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

</manifest>

and in addition i create a xml file named displaymessageactivity.xml in layout folder without this file i got an error in the main activity setcontentview command.

i say again my problem is : why i can not going back to mainActivity when i press Up button that is next to app icon. thanks

Try this in the switch case.

case android.R.id.home:
finish();
return true;
  1. Use lower case letters for package names in your manifest.eg com.example.action

  2. setDisplayHomeAsUpEnabled(true) in DisplayMessageActivity.java class to return you to your MainActivity class which is the parent activity.

  3. onOptionsItemSelected for your DisplayMessageActivity class remove case R.id.home and your switch default and return appropriately for the method. For example..`

.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_search:
            openSearch();
            return true;
        case R.id.action_settings:
            openSettings();
            return true; 
    }
    return super.onOptionsItemSelected(item);
}`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM