onNavigationItemSelected在动作条被称为在启动时怎样才能避免呢? [英] onNavigationItemSelected in ActionBar is being called at startup how can avoid it?

查看:496
本文介绍了onNavigationItemSelected在动作条被称为在启动时怎样才能避免呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的动作条有一个下拉菜单, onNavigationItemSelected()是一旦活动创建调用,所以第一个项目叫。我的下拉菜单的第一项是家庭,同样的动作为pressing与 android.R.id.home 所以,当应用程序启动时,它调用自身的应用程序图标。为了避免这种情况的发生我有这个code:

 如果(this.getClass()!= FrecView.class){//如果我不是目前的活动
    意图FREC =新的意图(这一点,FrecView.class);
    frec.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(FREC);
}
 

但我有动作条上所有的活动,让每家每户活动启动它时自称是永远的,所以我必须把那个code每一项活动。怎么了?我怎样才能prevent这种情况的发生?

解决方案
  

我使用的动作条白衣一个下拉菜单,并尽快活动创建onNavigationItemSelected()被调用

这不旨在是一个菜单,任何超过标签被设计为一个菜单。列表导航被设计为允许用户的,典型地通过更换一个片段,以指示某些内容的对于当前活动。操作项(如工具栏按钮,动作外溢区)是活动之间进行导航。

  

我的下拉菜单的第一项是家一样的动作为pressing应用程序图标丝毫android.R.id.home所以当应用程序启动时调用自身。

因此​​,删除从你的菜单。用户可以preSS您的应用程序图标左侧导航回家。

I am using ActionBar with a dropdown menu, and onNavigationItemSelected() is called as soon as the Activity is created, so the first item is called. The first item of my dropdown menu is Home, the same action as pressing the application icon with android.R.id.home so when application starts it calls itself. To avoid this from happening I have this code:

if(this.getClass() != FrecView.class){  //if i am not currently on the Activity
    Intent frec = new Intent(this, FrecView.class);
    frec.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(frec);
}

But i have ActionBar on all my activities so every time every activity is started it calls itself forever so I have to put that code for each activity. What is happening? How can i prevent this from happening?

解决方案

i am using ActionBar whit a dropdown menu and onNavigationItemSelected() is called as soon Activity is created

This is not designed to be a "menu", any more than tabs are designed to be a "menu". The list navigation is designed to allow the user to indicate some content for the current activity, typically by replacing a fragment. Action items (e.g., toolbar buttons, action spillover area) are for navigating between activities.

The first item of my dropdown menu is Home the same action as pressing the application icon whit android.R.id.home so when application starts it calls itself.

So, delete that from your "menu". The user can press your app icon on the left to navigate home.

这篇关于onNavigationItemSelected在动作条被称为在启动时怎样才能避免呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆