【问题标题】:Change bottom navigation item state on backpress from activity to home fragment将背压上的底部导航项状态从活动更改为主页片段
【发布时间】:2018-08-13 06:39:25
【问题描述】:

你好伟大的社区我有一个应用程序,我在其中使用带有 4 个片段的底部导航。要移动到最后一个片段,用户必须登录,因此当新用户打开应用程序并点击第 4 项时,会打开登录活动。 问题是当我从登录活动中退出时,它会将我移动到主页片段 1,这没问题,但底部导航项的状态不会改变片段 4 仍然突出显示。那么如何管理它,当我从登录活动返回时,应该突出显示主片段项目。

[

这是我的底部导航主要活动

public class Bottom_Nav extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

SparseArray<Fragment> myFragments;
String user_type;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bottom__nav);

    SharedPreferences sharedPreferences = getSharedPreferences("DataStore", Context.MODE_PRIVATE);
    user_type = sharedPreferences.getString("user_type", "");


    BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
    Nav_Helper.disableShiftMode(bottomNavigationView);

    //make array to avoid re creating of fragments
    myFragments = new SparseArray<Fragment>();

    //loading the default fragment
    loadFragment(new HomeFragment());

    //getting bottom navigation view and attaching the listener
    bottomNavigationView.setOnNavigationItemSelectedListener(this);


}

private void loadFragment(Fragment fragment) {

    //switching fragment
    FragmentTransaction fragmentTransaction =
            getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.content, fragment);
    fragmentTransaction.commit();

}


@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    Fragment fragment = null;

    int id = item.getItemId();

    if (id == R.id.navigation_home) {

            fragment = new HomeFragment();
            myFragments.put(1, fragment);


        loadFragment(fragment);

    } else if (id == R.id.navigation_use) {

        getFragmentManager().popBackStack();



                switch (user_type) {
                    case "detailer":

                        fragment = new Detailer_Zone();
                        myFragments.put(4, fragment);
                        loadFragment(fragment);
                        break;
                    case "customer":

                        fragment = new Customer_Zone();
                        myFragments.put(4, fragment);
                        loadFragment(fragment);
                        break;
                    case "empty":

                        Intent intent = new Intent(this, Login_Activity.class);
                        startActivity(intent);
                        break;
                }

            }


    else if (id == R.id.navigation_search) {
        // get cached instance of the fragment
        fragment = myFragments.get(2);

        // if fragment doesn't exist in myFragments, create one and add to it
        if (fragment == null) {
            fragment = new SearchFragment();
            myFragments.put(2, fragment);
        }

        loadFragment(fragment);

    } else if (id == R.id.navigation_map) {
        // get cached instance of the fragment
        fragment = myFragments.get(3);

        // if fragment doesn't exist in myFragments, create one and add to it
        if (fragment == null) {
            fragment = new MapsActivity();
            myFragments.put(3, fragment);
        }

        loadFragment(fragment);

    }
    return true;
}

【问题讨论】:

  • 用你的逻辑映射你onBackpress()
  • @Jaymin 如何从这里调用 public onNavigationItemSelected(@NonNull MenuItem item) 来改变底部菜单的状态??
  • 发布您的代码..
  • @pmms,使用startActivityForResult 并在用户按下返回按钮时返回RESULT_CANCELED,并在onActivityResult() 方法中处理Bottom_Nav 活动中的案例。
  • @Jaymin 你能不能给它添加一些代码sn-p

标签: android android-fragments bottomnavigationview


【解决方案1】:

登录案例:

 case "empty": 
        Intent intent = new Intent(this, Login_Activity.class);
        startActivityForResult(intent , 101);
        break; 

登录活动:

当用户点击后退按钮时,执行下面的代码。

setResult(RESULT_CANCELED);
finish(); 

Bottom_Nav活动中处理案例:

  // This method is called when the second activity finishes 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // check that it is the SecondActivity with an OK result 
    if (requestCode == 101) {
        if (resultCode == RESULT_CANCELED) {

           onNavigationItemSelected(menuItem)
        } 
    } 
} 

【讨论】:

  • 如何将我的菜单项 ID 或名称传递给 onNavigationSelected(?) 它会在此 onNavigationItemSelected(R.id.navigation_home); 时出错;
  • android.view.MenuItem 不能应用于 (int)
【解决方案2】:

试试这个解决方法。用这个替换你的case "empty":

case "empty":
    bottomNavigationView.setSelectedItemId(R.id.navigation_home);
    Intent intent = new Intent(this, Login_Activity.class);
    startActivity(intent);
    break;

告诉我这是否有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-14
    • 2017-08-04
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    相关资源
    最近更新 更多