OnNavigationItemsSelected 侦听器一次又一次地启动相同的活动

Posted

技术标签:

【中文标题】OnNavigationItemsSelected 侦听器一次又一次地启动相同的活动【英文标题】:OnNavigationItemsSelected Listener is starting the same activity again and again 【发布时间】:2018-11-29 03:55:48 【问题描述】:

我是 android 开发新手,我正在尝试使用 bottomNavigationView 创建一个应用程序。我创建了一个单独的类来为不同的活动设置bottomNavigationView,这样我就不必一次又一次地编写代码。但是,当我在 Android 设备中启动该应用程序时,无论我单击什么项目,它都会再次启动 Welcome 活动,它会启动 Welcome 活动。这是我的 navigationHelperClass

public class BottomNavigationViewHelper 
private static final String TAG = "BottomNavigationViewHel";

public static void setUpNavigationView(BottomNavigationViewEx bottomNavigationViewEx)
    Log.d(TAG, "setUpNavigationView: setting BottomNavigation");
    bottomNavigationViewEx.enableAnimation(false);
    bottomNavigationViewEx.enableItemShiftingMode(false);
    bottomNavigationViewEx.enableShiftingMode(false);
    bottomNavigationViewEx.setTextVisibility(false);


public static void enableNavigation(final Context context, final BottomNavigationViewEx viewEx)
    viewEx.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() 
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) 
            switch(item.getItemId())
                case R.id.btnHome:
                    viewEx.setSelectedItemId(R.id.btnHome);
                    Intent intent = new Intent(context, Welcome.class);
                    context.startActivity(intent);
                    break;
                case R.id.btnSearch:
                    Intent intent1 = new Intent(context, Chats.class);
                    context.startActivity(intent1);
                    break;
                case R.id.btnPost:
                    Intent intent2 = new Intent(context, Posts.class);
                    context.startActivity(intent2);
                    break;
                case R.id.btnFavourites:
                    Intent intent3 = new Intent(context, Favourites.class);
                    context.startActivity(intent3);
                    break;
                case R.id.btnProfile:
                    Intent intent4 = new Intent(context, Profile.class);
                    context.startActivity(intent4);
                    break;
            
            return false;
        
    );



这是我的欢迎活动,当我单击底部导航视图的任何项目时开始。

公共类欢迎扩展 AppCompatActivity 私有上下文 mCntext = Welcome.this;

private static final String TAG = "Welcome";
BottomNavigationViewEx bottomNav;

@Override
protected void onCreate(Bundle savedInstanceState)
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
    Log.d(TAG, "onCreate: starting");
    setupBottomNavigationView();

    Menu menu = bottomNav.getMenu();
    MenuItem menuItem = menu.getItem(0);
    menuItem.setChecked(true);

private void setupBottomNavigationView()
    Log.d(TAG, "setupBottomNavigationView: setting bottomnavigationview");
    bottomNav = findViewById(R.id.nav_bottom);
    BottomNavigationViewHelper.setUpNavigationView(bottomNav);
    BottomNavigationViewHelper.enableNavigation(mCntext, bottomNav);


这是我的活动之一,其余活动的代码也相同。这是个人资料活动。

public class Profile extends AppCompatActivity 

private Context mContext = Profile.this;
private static final String TAG = "Search";
BottomNavigationViewEx bottomNav;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    setupBottomNavigationView();



private void setupBottomNavigationView()
    Log.d(TAG, "setupBottomNavigationView: setting bottomnavigationview");
    bottomNav = findViewById(R.id.nav_bottom);
    BottomNavigationViewHelper.setUpNavigationView(bottomNav);
    BottomNavigationViewHelper.enableNavigation(mContext, bottomNav);
    Menu menu = bottomNav.getMenu();
    MenuItem menuItem = menu.getItem(4);
    menuItem.setChecked(true);

@Override
public void setTitle(CharSequence title) 


【问题讨论】:

【参考方案1】:

删除以下行:

viewEx.setSelectedItemId(R.id.btnHome);

来自case R.id.btnHome: 下的onNavigationItemSelected 回调案例。

【讨论】:

【参考方案2】:

不要对活动使用底部导航,而是使用片段。创建一个将保存您的片段的父活动,然后在 onNavigationItemSelectListener 中更改片段,在这种情况下,您不必管理底部导航状态和所选项目。你可以在这里查看如何更改片段How to change fragment with the Bottom Navigation Activity?

【讨论】:

以上是关于OnNavigationItemsSelected 侦听器一次又一次地启动相同的活动的主要内容,如果未能解决你的问题,请参考以下文章