使用向上按钮导航回相应的片段

时间:2017-03-01 14:25:59

标签: java android

我使用三个顶级目的地(每个目的地一个片段)实施了底部导航:主页搜索设置。在搜索片段中,我有一个搜索按钮,在点击后,转到另一个活动说ActivityX。使用向上按钮从ActivityX导航回来时,它会导航到Home而不是Search。我希望向上按钮导航到搜索。如何实现呢?

我的主要活动:

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

    sp=getSharedPreferences("login_details",MODE_PRIVATE);
    if(!sp.getBoolean("login_status", false)){
        isLoggedIn=false;
    }
    else
        isLoggedIn=true;

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    BottomNavigationView bottomNav = (BottomNavigationView) findViewById(R.id.navigation);
    fragmentManager=getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction ft1=fragmentManager.beginTransaction();
    ft1.add(R.id.container,new HomeFragment());
    ft1.commit();
    updateToolbarText("Home");
    bottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            if(item.getItemId()==R.id.action_home){
                fragmentManager=getSupportFragmentManager();
                android.support.v4.app.FragmentTransaction ft1=fragmentManager.beginTransaction();
                ft1.replace(R.id.container,new HomeFragment());
                ft1.commit();

                updateToolbarText("Home");

            }
            if(item.getItemId()==R.id.action_search){
                fragmentManager=getSupportFragmentManager();
                android.support.v4.app.FragmentTransaction ft2=fragmentManager.beginTransaction();
                ft2.replace(R.id.container,new SearchFragment());
                ft2.commit();
                MainActivity.super.onBackPressed();
                updateToolbarText("Search");


            }
            if(item.getItemId()==R.id.action_settings){
                fragmentManager=getSupportFragmentManager();
                android.support.v4.app.FragmentTransaction ft3=fragmentManager.beginTransaction();
                ft3.replace(R.id.container,new SettingsFragment());
                ft3.commit();
                updateToolbarText("Settings");
            }
            return true;

        }

    });

}

1 个答案:

答案 0 :(得分:0)

在ActivityX中创建intent并将字符串放入其中。该字符串包含您来自哪里的信息,即。搜索,然后startActivity(ActivityX,MainActivity)。

然后在MainActivity中覆盖onResume方法。在此方法中,检查intent是否包含您放入ActivityX的特定字符串。如果它包含该字符串,请启动SearchFragment

以下是我的例子:

ActivityX:

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent(FragmentTrainingDetail.this, MainActivity.class);
    intent.putExtra("TAG", "FragmentMyTraining");
    startActivity(intent);
    finish();
}

MainActivity:

@Override
protected void onResume() {
    super.onResume();

    Intent intent = getIntent();

    if (intent.hasExtra("FragmentMyTraining")) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.content_frame, new FragmentMyTraining());
        fragmentTransaction.addToBackStack("my");
        fragmentTransaction.commit();
    } else if (intent.hasExtra("FragmentPopularTraining")) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.content_frame, new FragmentPopularTraining());
        fragmentTransaction.addToBackStack("pop");
        fragmentTransaction.commit();
    }
}