Android 工具栏 return 按钮在添加 productFlavors 后不起作用

Android Toolbar return button didn't work after adding productFlavors

build.gradle(Module:app) 中添加 productFlavors 之前,Toolbar 中的 return 按钮有效。 但是一旦我将 productFlavors 添加到 build.gradle(Module:app) for Build >> Generate Signed APK...,return 按钮就失效了。

AndroidManifest.xml:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="AllowBackup,GoogleAppIndexingWarning">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".NoteEditActivity">
            <meta-data android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity"/>
        </activity>
    </application>

</manifest>

build.gradle(Module:app) 添加后 productFlavors:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "com.comp548.note"
        minSdkVersion 19
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
        free {
            applicationId "com.comp548.note.free"
            versionName "1.0-free"
        }
        paid {
            applicationId "com.comp548.note.paid"
            versionName "1.0-paid"
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.support:design:24.2.0'
}

toolbar.xml 在 layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/colorPrimary"
    app:theme="@style/ThemeOverlay.AppCompat.Dark"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    tools:ignore="Overdraw">

</android.support.v7.widget.Toolbar>

activity_note_edit.xml 在 layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.comp548.note.NoteEditActivity">

    <include layout="@layout/toolbar"
        android:id="@+id/note_edit_toolbar" />

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/fragment_margin"
        android:name="com.comp548.note.NoteEditFragment"
        android:id="@+id/fNoteEdit"
        android:layout_centerHorizontal="true"
        tools:layout="@layout/fragment_note_edit"
        android:layout_below="@id/note_edit_toolbar"/>
</RelativeLayout>

MainActivity.java:

package com.comp548.note;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

        Toolbar toolbar = (Toolbar)findViewById(R.id.note_list_toolbar);
        // Set the Toolbar to act as the ActionBar for this Activity window
        setSupportActionBar(toolbar);
        if(getSupportActionBar() != null) {
            getSupportActionBar().setTitle(R.string.toolbar_title);
        }
        getSupportActionBar().setIcon(R.drawable.ic_toolbar);

        FloatingActionButton fabAdd = (FloatingActionButton)findViewById(R.id.fabAdd);
        fabAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, NoteEditActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu_exit:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

NoteEditActivity.java:

package com.comp548.note;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;

public class NoteEditActivity extends AppCompatActivity {

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

        Toolbar toolbar = (Toolbar)findViewById(R.id.note_edit_toolbar);
        // Set the Toolbar to act as the ActionBar for this Activity window
        setSupportActionBar(toolbar);
        // Enable return button
        if(getSupportActionBar() != null) {
            getSupportActionBar().setHomeButtonEnabled(true);
        }
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        Intent intent = getIntent();
        NoteEntity noteEntity = (NoteEntity) intent.getSerializableExtra("noteEntity");
        // Set values for Views in NoteEditFragment
        if (noteEntity != null) {
            NoteEditFragment noteEditFragment = (NoteEditFragment)getSupportFragmentManager().findFragmentById(R.id.fNoteEdit);
            noteEditFragment.setValues(noteEntity);
        }
    }
}

应用截图:

build.gradle(Module:app) 中删除 productFlavors 后,return 按钮再次起作用。

为什么 Toolbar 上的 return 按钮在添加 productFlavors 后不起作用。

如果我还想添加 productFlavors 如何解决?

好的 NoteEditActivity 添加回调方法 onOptionsItemSelected(MenuItem item) 并执行以下操作:

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case android.R.id.home:
            onBackPressed();
            break;
    }
    return super.onOptionsItemSelected(item);
}

它应该可以解决您的问题,允许工具栏后退按钮执行后退按钮的操作。希望这有帮助。

原因:您为每种口味设置了不同的applicationId。 在清单中,您为父 activity 设置了元数据,但在构建风味期间,所有包都会自动移动到包 com.comp548.note.freecom.comp548.note.paid。你的元数据指向 com.comp548.note.MainActivity 虽然你的实际 activity 位于 com.comp548.note.paid|free.MainActivity.

解决方案:

  1. 使用元数据点为每种风格创建单独的清单以更正包。
  2. KudzieChase 描述的那样在运行时处理它。