没有自定义工具栏,向上导航按钮在基本Android应用程序中不起作用

时间:2015-09-09 10:25:01

标签: java android android-toolbar

我'应用工具栏的UP按钮出现问题,在子活动中按下时,该按钮永远不会通过R.id.home方法上的onOptionsItemSelected

主要活动

package com.foocorp.firstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class InstrumentListActivity extends AppCompatActivity
        implements IUdpNotificationListener
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_my);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();


        if (id == R.id.action_settings) {
            return true;
        }
        if (id == R.id.home) {
            Toast.makeText(getApplicationContext(), "Clicked Menu back from Main", Toast.LENGTH_SHORT).show();
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {

        Toast.makeText(getApplicationContext(), "Clicked Hard back from Main", Toast.LENGTH_SHORT).show();
        finish();
    }
}

我设置setDisplayHomeAsUpEnabled(true);

的子活动
package com.foocorp.firstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class InstrumentViewDetailsActivity extends AppCompatActivity implements IUdpNotificationListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_instrument_view_details);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        Intent intent = getIntent();

        //whatever here
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_instrument_view_details, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();


        if (id == R.id.action_settings) {
            return true;
        }
        //=> never get called because passed ID is never R.id.home
        if (id == R.id.home) {
            Toast.makeText(getApplicationContext(), "Clicked Menu back from Details", Toast.LENGTH_SHORT).show(); 
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {

        Toast.makeText(getApplicationContext(), "Clicked Hard back from Details", Toast.LENGTH_SHORT).show(); // this works fine
        finish();
    }

}

定义父子关系的清单文件

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.foocorp.firstapp">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_tap_and_play_white_24dp"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name="com.foocorp.firstapp.InstrumentListActivity"
            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=".InstrumentViewDetailsActivity"
            android:label="@string/instrum_details"
            android:parentActivityName="com.foocorp.firstapp.InstrumentListActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.foocorp.firstapp.InstrumentListActivity" />
        </activity>
    </application>

</manifest>

子活动的菜单布局文件(顺便说一下,虽然在应用程序运行时出现了UP按钮,但这是正常的吗?)

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.foocorp.firstapp.InstrumentViewDetailsActivity">

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never" />
</menu>

在互联网上搜索此解决方案时,我在清单文件和getSupportActionBar().setHomeButtonEnabled(true);中找到了关于父/子关系的提示,但是尽管有这些提示,我无法通过此UP工具栏按钮正常工作:它返回到父活动,但不是以正确的方式,如我按下后退按钮调用onBackPressed()方法,并且工作正常。

关于我在这里失踪的任何线索?

1 个答案:

答案 0 :(得分:2)

更改后退按钮的ID if(player.bounds.overlaps(enemy1.bounds)){ game.setScreen(game.gameOver); } 方法中的R.id.homeandroid.R.id.home

您也可以使用onOptionsItemSelected()代替NavUtils.navigateUpFromSameTask(this);,因为您在finish();中提及了parentActivityName