片段运行中的后退键在主要活动中起作用

时间:2018-07-16 14:22:13

标签: java android android-fragments

我有一个活动,该活动调用一个片段,如下所示:

主要活动中的导航抽屉:

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    android.app.FragmentManager fragmentManager = getFragmentManager();

    if(id == R.id.nav_jobs_layout){
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, new JobsFragment())
                .addToBackStack("Jobs")
                .commit();
    }else if(id == R.id.nav_shipments_out){
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, new ShipmentsOutFragment())
                .addToBackStack("ShipmentsOut")
                .commit();
    }else if(id == R.id.logout){
        SharedPreferences preferences = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
        Authentication auth = new Authentication(preferences);
        auth.logout();

        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        startActivity(intent);
    }
}

当我单击“货件”时,它会打开我的货件片段,其中有一个货件列表供用户选择。根据选择,我打开另一个片段,该片段将显示在此处。这是在这里完成的:

ShipmentFragment:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Shipment shipment = shipments.get(position);

        android.app.FragmentManager fragmentManager = getFragmentManager();

        ShipmentsOutDetailsFragment newFragment = new ShipmentsOutDetailsFragment();

        newFragment.shipment = shipment;

        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, newFragment)
                .addToBackStack("Shipments")
                .commit();
    }
});

在下一个片段中,我将显示所选货件的详细信息。当您单击该片段时,就会出现问题。就像每次我重新加载基本货件时加载第一个片段一样。

当您单击返回时,似乎正在运行onBackPressed函数,如此处所示(在主活动中)。我怎样才能阻止它运行?我可以在两个片段中放一个覆盖吗?

这是主要活动中令人讨厌的onBackPressed,当我单击第二个片段但我单击的第一个片段时,我不想让它运行。

@Override
public void onBackPressed() {
    DrawerLayout drawer = findViewById(R.id.drawer_layout);

    Log.v("BackPressed", "This function has been run");

    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();

        this.finish();
    }
}

2 个答案:

答案 0 :(得分:1)

您可以声明两个领域:

 final static int JOBS_FRAGMENT = 0;
 final static int SHIPMENTS_OUT_FRAGMENT = 1;

还有另一个variable,您可以在其中保存显示的当前fragment

 public int currentFragmentShown = JOBS_FRAGMENT; // Or add the fragment shown by default on the start of the activity

然后每次更改currentFragmentShwon时更改fragment的值:

    if(id == R.id.nav_jobs_layout){
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, new JobsFragment())
                .addToBackStack("Jobs")
                .commit();
        currentFragmentShown = JOBS_FRAGMENT;
    }else if(id == R.id.nav_shipments_out){
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, new ShipmentsOutFragment())
                .addToBackStack("ShipmentsOut")
                .commit();
        currentFragmentShown = SHIPMENTS_OUT_FRAGMENT;
    }

然后可以执行以下inide onBackPressed()函数:

@Override
public void onBackPressed() {

 if (currentFragmentShown == JOBS_FRAGMENT) {
        //Do whatever you want to do when jobs fragment is shown
    } else {
        //Do whatever you want to do when shippement out fragment fragment is shown
    }
}

这可能不是最好的解决方案,但我希望这可以有所帮助

答案 1 :(得分:1)

请尝试

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            int count = getFragmentManager().getBackStackEntryCount();
            if (count == 0) {
                super.onBackPressed(); //this method just will be called when there is none more fragments
            } else getFragmentManager().popBackStack();
        }
    }