onOptionsItemSelected is not invoked both in the Activity and the Fragment, even though I implemented the methods as described here: Add onOptionsItemSelected calling in Fragment
I have an activity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu); // false by default. so goes to fragment
// If return true, than stay in the activity
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "onOptionsItemSelected", Toast.LENGTH_SHORT).show();
switch(item.getItemId())
{
default:
return super.onOptionsItemSelected(item);
// false by default. so goes to fragment
// If returns true stays in activity.
}
}
and this is the fragment that doesn't work:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);((AppCompatActivity)activity).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_edit_mode,menu);
setHasOptionsMenu(true);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(activity,"onOptionItemSelected", Toast.LENGTH_SHORT).show(); // Not Invoked!!!
if (!isDataInitialized()) return true;
switch (item.getItemId()) {
case R.id.item_share: // Share Icon
callUsernamesDialog();
return true;
case android.R.id.home: // Back pressing
( (ActivityProfile)activity).onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
It's onOptionsItemSelected is not called but the menu is inflated successfully.
In other fragments everything works fine and I don't see any difference in the implementation.
In the other fragments onOptionsItemSelected is invoked both in the activity and the fragment. What can I look for in this particular fragment that can prevent it from working ?
A help would be appreciated!!! Thanks in advance!
UPDATE
I found the solution and now the items clicks are available but I have a question on that.
The problem was that I used a scrollView in the fragment because I have many elements in it:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fragment_edit_data_layout"
tools:context=".UserStuff.EditData.FragmentEditData"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:fontFamily="cursive"
android:lineSpacingExtra="10sp"
android:text="@string/edit_data"
android:textAlignment="center"
android:textAllCaps="false"
android:textAppearance="@style/TextAppearance.AppCompat.Button"
android:textColor="@color/colorPrimary"
android:textSize="60sp"
android:textStyle="bold"
android:typeface="serif" />
<EditText
android:id="@+id/et_title"
style="@android:style/Widget.AutoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="100dp"
android:fontFamily="serif"
android:hint="@string/title"
android:inputType="textEmailAddress"
android:textAppearance="@style/TextAppearance.AppCompat.Caption"
android:textColor="@android:color/background_dark"
android:textSize="18sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
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"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:text="@string/folder_category"
/>
<android.support.v7.widget.AppCompatButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="@dimen/standard_21"
android:layout_gravity="center"
android:id="@+id/btn_category"
android:text="@string/untitled"
android:textSize="14sp"
/>
<android.support.v7.widget.AppCompatButton
android:layout_margin="8dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="14sp"
android:text="@string/refresh"
android:id="@+id/btn_refresh"
/>
</LinearLayout>
<EditText
android:id="@+id/et_description"
style="@android:style/Widget.AutoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="100dp"
android:fontFamily="serif"
android:hint="@string/description"
android:inputType="textEmailAddress"
android:layout_margin="8dp"
android:textAppearance="@style/TextAppearance.AppCompat.Caption"
android:textColor="@android:color/background_dark"
android:textSize="18sp" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_edit_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorPrimary"
android:text="@string/tap_to_edit"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="@color/cardview_light_background"
android:textSize="24sp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_edit_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</LinearLayout>
</ScrollView>
Removing the ScrollView and using the LinearLayout as the root view solved the problem, the question is Why ? I can't see docs on that. Thanks!