简体   繁体   English

操作栏上的主页按钮

[英]Home Button on action bar

I am new to android development and was following the " Adding Action Bar Tutorial " 我是android开发的新手,并且正在关注“ 添加操作栏教程

I was trying to get the home (back carrot) button on the action bar to enable my app to go back to the main activity. 我试图使操作栏上的主页(后退胡萝卜)按钮使我的应用返回到主要活动。

It is mentioned in the tutorial that this can be done without handling the up button event, if the parent is defined in the xml. 在本教程中提到,如果父项是在xml中定义的,则无需处理向上按钮事件就可以完成此操作。

below is my xml file 以下是我的xml文件

<activity
    android:name="com.example.firstapp.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>

but pressing the carrot button does not work. 但是按胡萝卜按钮不起作用。 what can i be doing wrong? 我该怎么办?

EDIT: adding the activity code below 编辑:在下面添加活动代码

public class DisplayMessageActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*
         * setContentView(R.layout.activity_display_message); if
         * (savedInstanceState == null) {
         * getSupportFragmentManager().beginTransaction() .add(R.id.container,
         * new PlaceholderFragment()).commit(); }
         */

        // 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);

        // Set the text view as the activity layout
        setContentView(textView);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    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.
        /*
         * switch (item.getItemId()) { case android.R.id.home: Intent intent =
         * new Intent(getApplicationContext(),
         * com.example.firstapp.MainActivity.class);
         * intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         * startActivity(intent); return true; default: return
         * super.onOptionsItemSelected(item); }
         */
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_display_message,
                    container, false);
            return rootView;
        }
    }

}

MainActivity is below MainActivity在下面

public class MainActivity extends ActionBarActivity {

    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        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();
                EditText temp1 = (EditText) findViewById(R.id.edit_message);
                temp1.setText("Search");
                return true;
            case R.id.action_settings:
                // openSettings();
                EditText temp2 = (EditText) findViewById(R.id.edit_message);
                temp2.setText("Settings");
                return true;
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /** Called when the user clicks the Send button */
    public void sendMessage(View view) {
        // Do something in response to button
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

Thanks 谢谢

@Newton It's actually redundant to call ActionBar.setDisplayHomeAsUpEnabled(true), if you're using the parentActivityName attribute.. That being said, when you remove that line from your DisplayMessageActivity, is the "up" affordance still present? @Newton如果您使用parentActivityName属性,则实际上调用ActionBar.setDisplayHomeAsUpEnabled(true)是多余的。也就是说,当您从DisplayMessageActivity中删除该行时,“向上”提供功能仍然存在吗? – adneal –肾上腺

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked; go home
            Intent intent = new Intent(this, HomeActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

在您的活动中使用

    getActionBar().setDisplayHomeAsUpEnabled(true); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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