[英]Menu fragment class not being called
I wish control my menu through a Fragment in order to minimise code duplication. 我希望通过片段控制菜单,以最大程度地减少代码重复。 The problem I have is that the activity class receives the call when debugging, but it does not continue to the
Fragment
class. 我的问题是,活动类在调试时会收到调用,但不会继续进入
Fragment
类。 Classes shown below. 下面显示的类。
I thought that the Fragment 'participates' in the layout lifecycle when inheriting from Fragment. 我以为Fragment从Fragment继承时会“参与”布局生命周期。 What am I missing here?
我在这里想念什么?
public class SomeActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().show();
setContentView(R.layout.a);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
public void chooseLanguage(View view) {
Intent intent = new Intent(this, AActivity.class);
startActivity(intent);
}
}
public class MenuFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menuInflater.inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu, menuInflater);
}
public void startActivity(Class classs) {
Intent intent = new Intent(getActivity(), classs);
startActivity(intent);
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.a:
startActivity(AActivity.class);
return true;
case R.id.b:
startActivity(BActivity.class);
return true;
case R.id.c:
startActivity(CActivity.class);
return true;
case R.id.d:
startActivity(DActivity.class);
return true;
default:
return super.onOptionsItemSelected(menuItem);
}
}
}
From documentioned of onCreateOptionsMenu() of Activity
: 从记录的
Activity
的onCreateOptionsMenu() :
You must return true for the menu to be displayed;
您必须返回true才能显示菜单。 if you return false it will not be shown.
如果返回false,则不会显示。
So make sure to return true instead of 因此,请确保返回true而不是
return super.onCreateOptionsMenu(menu);
So the solution consists of 所以解决方案包括
Adding the Fragment in the Activity's onCreate
在活动的
onCreate
添加片段
getFragmentManager().beginTransaction().add(new MenuFragment(), "Menu fragment").commit();
Then onOptionsItemSelected
in the Activity should return false as according to the documentation 然后,根据文档中的说明,Activity中的
onOptionsItemSelected
应该返回false
boolean Return false to allow normal menu processing to proceed, true to consume it here.
https://developer.android.com/reference/android/app/Activity.html#onOptionsItemSelected(android.view.MenuItem) https://developer.android.com/reference/android/app/Activity.html#onOptionsItemSelected(android.view.MenuItem)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.