activity 开始后立即设置操作栏项目可见性很热吗?
Hot to set Action Bar item visibility as soon as activity starts?
当 Activity 重新启动时,一切正常,但我想在 Activity 启动时设置菜单项可见性。
private boolean userIsCompany = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment vacancyListFragment = new VacancyListFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_frame,
vacancyListFragment).commit();
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
final GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
如果 Firebase 中的用户有 child , boolean userIsCompany 将变为 true;
// Check if current user is CompanyUser.
// If it is addVacancy button will become visible.
assert account != null;
companiesUidRef = mRef
.child("Companies")
.child(Objects.requireNonNull(account.getId()));
companiesUidRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
userIsCompany = true;
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
这是 OncreateOptionsMenu 方法
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar_menu, menu);
MenuItem addVacancy = menu.findItem(R.id.action_add_vacancy);
if(!userIsCompany){
addVacancy.setVisible(false);
}
return true;
}
在 onCreateOptionsMenu 完成菜单初始化之前,变量 userIsCompany
似乎不会为真。解决此问题的方法是在将 userIsCompany 设置为 true 后添加对 invalidateOptionsMenu()
的调用。
这将再次调用 onCreateOptionMenu 并更新其可见性。
当 Activity 重新启动时,一切正常,但我想在 Activity 启动时设置菜单项可见性。
private boolean userIsCompany = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment vacancyListFragment = new VacancyListFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_frame,
vacancyListFragment).commit();
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
final GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
如果 Firebase 中的用户有 child , boolean userIsCompany 将变为 true;
// Check if current user is CompanyUser.
// If it is addVacancy button will become visible.
assert account != null;
companiesUidRef = mRef
.child("Companies")
.child(Objects.requireNonNull(account.getId()));
companiesUidRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
userIsCompany = true;
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
这是 OncreateOptionsMenu 方法
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar_menu, menu);
MenuItem addVacancy = menu.findItem(R.id.action_add_vacancy);
if(!userIsCompany){
addVacancy.setVisible(false);
}
return true;
}
在 onCreateOptionsMenu 完成菜单初始化之前,变量 userIsCompany
似乎不会为真。解决此问题的方法是在将 userIsCompany 设置为 true 后添加对 invalidateOptionsMenu()
的调用。
这将再次调用 onCreateOptionMenu 并更新其可见性。