EDITED
Thanks to all that voted me negative without trying help. That is really useful.
This main problem was I had 2 same fragments, one in main and other in the premium flavor. So the system was loading always the fragment stored in main and because of that the button listener is never called.
You can solve it removing the fragment from main path and create the fragment in every flavor you have, by this way the proper fragment will be selected
If you want to have a different version of the same class in the two flavor you'll need to create it in both flavors and remove it from main.
src/flavor1/java/com/foo/A.java src/flavor2/java/com/foo/A.java
And then your code in
src/main/java
can do:import com.foo.A
depending on the flavor selected, the right version of
com.foo.A
is used.
ORIGINAL QUESTION
I'm trying to set a button in a "premium" flavor that is not shown in the main flavor. The problem is the button is shown in the UI, but the onClickListener is never fired.
My structure is the following:
I have an activity that contains a fragment. The activity is unique for all flavor, but the fragment is going to change depends of the flavor. One of that flavor contains a button that is not shown in the main flavor. When tap on that button, nothing happens.
Here is the code related:
The activity xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
...
android:orientation="vertical"
android:weightSum="10"
tools:context=".activities.ConfigurationActivity">
<ImageView
...
android:layout_weight="2"/>
<FrameLayout
android:id="@+id/configurationFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"/>
</LinearLayout>
The activity code is:
package com.abc.project.activities;
public class ConfigurationActivity extends AppCompatActivity {
private Fragment confFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_configuration);
confFragment = new ConfigurationFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.configurationFragment, confFragment)
.commit();
}
}
The main fragment xml is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:orientation="vertical"
tools:context="com.abc.project.fragments.ConfigurationFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Main fragment"/>
</LinearLayout>
The main fragment code is:
package com.abc.project.fragments;
public class ConfigurationFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_configuration, container, false);
return view;
}
}
The premium fragment xml is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:orientation="vertical"
tools:context="com.abc.project.premium.fragments.ConfigurationFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Premium fragment!"/>
<Button
android:id="@+id/premiumButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button premium"/>
</LinearLayout>
And the premium fragment code is:
package com.abc.project.premium.fragments;
public class ConfigurationFragment extends Fragment {
private Button premiumButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_configuration, container, false);
premiumButton = (Button)view.findViewById(R.id.premiumButton);
premiumButton.setOnClickListener(premiumOnClickListener);
return view;
}
private View.OnClickListener premiumOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
//THIS CODE NEVER IS FIRED
}
};
}
So, the "premium" xml is showed properly, but the button does not fire the onClickListener.
Anyone knows why is that happening?
Thanks in advance.
Regards, Kemmitorz.