invalidateOptionsMenu()在“重新启动”活动时不起作用
我有一个活动,我们称它为FirstActivity.java ,它具有一个带有通过片段实例化的导航抽屉的actionBar 。 在这个片段中, NavigationDrawerFragment.java ,我已经将任何通常为了实例化actionBar的onCreateOptionsMenu(菜单菜单,MenuInflater inflater),onOptionsItemSelected(MenuItem项目)和onPrepareOptionsMenu(菜单菜单)放在方法中。 在actionBar的右侧,我有一个带有“messages”图标的按钮,这个图标通向用户的收件箱片段。 当收到新消息时,我在FirstActivity中调用invalidateOptionsMenu(),并将图标更改为onPrepareOptionsMenu(菜单菜单)中较亮的一个。 到目前为止一切正常。
问题是:我可以通过导航抽屉中的一个选项离开FirstAcitivty。 我通过Intent来完成。 我们称之为SecondActivity.java 。
Intent myIntent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(myIntent);
finish();
当我再次通过Intent调用FirstAcitivity时,invalidateOptionsMenu()方法停止工作。 它不触发onCreateOptionsMenu和onPrepareOptionsMenu方法。 我也试过supportInvalidateOptionsMenu()没有运气。
只有当我点击MENU按钮(在actionBar的左侧)时,才会调用这些方法,并且更改消息图标。 有人知道这种奇怪行为的原因吗?
更好的是,有没有人知道我的问题的解决方案? :)
FirstActivity.java
public class FirstActivity extends ActionBarActivity impements NavigationDrawerFragment.NavigationDrawerCallbacks {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
// When a new message arrives
if (thereIsAnIncomingMessage) {
invalidateOptionsMenu();
}
}
}
first_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:id="@+id/first_activity_layout">
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FirstActivity">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout android:id="@+id/container" android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.example.Controller.Main.NavigationBar.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
NavigationDrawerFragment.java
public class NavigationDrawerFragment extends Fragment {
private RelativeLayout relativeLayout;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
relativeLayout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
return relativeLayout;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.menu_main, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_menu) {
// Open navigation drawer menu
return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
} else if (item.getItemId == R.id.action_chat) {
// Open messages fragment
MessagesFragment messagesFragment = new MessagesFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, messagesFragment, "MESSAGES_FRAGMENT");
fragmentTransaction.addToBackStack("chatFrag");
fragmentTransaction.commit();
}
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
if (FirstActivity.thereIsAnIncomingMessage) {
menu.getItem(0).setIcon(R.drawable.new_message_icon);
} else {
menu.getItem(0).setIcon(R.drawable.message_icon);
}
}
public interface NavigationDrawerCallbacks {
void onNavigationDrawerItemSelected(int position);
}
}
问题是当您点击后退按钮时,不会调用FirstActivity
的OnCreate
方法。 相反,你可以把你的代码放在OnResume
,如下所示:
@Override
public void onResume(){
super.onResume();
if(thereIsAnIncomingMessage)
invalidateOptionsMenu();
}
请参阅官方的Activity Lifecycle文档。
链接地址: http://www.djcxy.com/p/63919.html上一篇: invalidateOptionsMenu() not working when "restarting" activity
下一篇: Hide MenuItem in one Fragment in FragmentActivity with swipe tabs functionality