来自另一个活动的BottomNavigationView

时间:2019-03-20 12:20:51

标签: android android-activity fragment bottomnavigationview

我有MainActivity,其中包含片段(例如3个片段),我可以通过BottomNavigationView选择片段。

我还在OneFragment中有一个按钮,该按钮启动SecondActivity(它也包含BottomNagigationView)。

enter image description here

我的问题:当我在SecondActivity中选择bottomNV itemId两个时,我需要选择TwoTwo返回到MainActivity。

enter image description here

enter image description here

我该如何实现?

MainActivity.class

public class MainActivity extends AppCompatActivity {

private BottomNavigationView navigationView;
private Toolbar toolbar;

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

    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    navigationView = findViewById(R.id.navigationBottom);
    navigationView.setOnNavigationItemSelectedListener(navListelener);
    getSupportFragmentManager().beginTransaction().replace(R.id.content_main,
            new OneFragment()).commit();
}

public BottomNavigationView.OnNavigationItemSelectedListener navListelener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        setTitle(item.getTitle());
        switch (item.getItemId()){
            case R.id.bottom_one:
                selectFragment(new OneFragment());
                break;
            case R.id.bottom_two:
                selectFragment(new TwoFragment());
                break;
            case R.id.bottom_three:
                selectFragment(new ThreeFragment());
                break;

        }
        return true;
    }
};

private void selectFragment(Fragment selectedFragment){
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.content_main, selectedFragment)
            .commit();
}

}

SecondActivity.class

public class SecondActivity extends AppCompatActivity {

private BottomNavigationView navigationView;

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

    navigationView = findViewById(R.id.navigationBottom);
    navigationView.setOnNavigationItemSelectedListener(navListener);

    Menu menu = navigationView.getMenu();
    MenuItem menuItem = menu.getItem(1);
    menuItem.setChecked(true);
}

private BottomNavigationView.OnNavigationItemSelectedListener navListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.bottom_one:
                finish();
                break;
            case R.id.bottom_two:
                //need to return to MainActivity with selected TwoFragment
                break;
            case R.id.bottom_three:
                break;
        }

        return true;
    }
};
}

2 个答案:

答案 0 :(得分:1)

我在您的代码中看到第二个活动称为SecondaActivity而不是SecondActivity。我以为它拼写错误,我的代码反映了最后一个。

您可以使用开始第二个活动(someRequestCode只是您可以选择的随机整数):

Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent, someRequestCode);

然后在第二个活动中,您可以在情况2下返回OK,在其他情况下可以返回KO(为简单起见。如果您想进一步说明,您可以添加一些额外内容):

case R.id.bottom_two:
   //need to return to MainActivity which selected TwoFragment
   Intent data = new Intent();
   data.putExtra("key", "value"); // You can add data if needed. For example, number of fragment to be changed
   setResult(RESULT_OK, data);
   finish();
   break;

最后在您的MainActivity中,重写onActivityResult(someRequestCode与前面所述的值相同):

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == someRequestCode) {
        if (resultCode == RESULT_OK) {
            // Here you can get key, value pair from extra and act accordingly
            selectFragment(new TwoFragment());
        } else {
            // Do something else
        }
    }
}

答案 1 :(得分:0)

case R.id.bottom_two:
            //need to return to MainActivity which selected TwoFragment
   Intent intent = new Intent(SecondaActivity.this,MainActivity.class);
   intent.puExtra("fragment_no",2);
   startActivity(intent);
   finish();
   break;

在MainActivity中

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

toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navigationView = findViewById(R.id.navigationBottom);
navigationView.setOnNavigationItemSelectedListener(navListelener);
Bundle extras = getIntent().getExtras(); 
if(extras!=null)
{   intent number = extras.getInt("fragment_no");
if(number == 2)
getSupportFragmentManager().beginTransaction().replace(R.id.content_main,
        new TwoFragment()).commit();
}
else
getSupportFragmentManager().beginTransaction().replace(R.id.content_main,
        new OneFragment()).commit();
}