如何从片段1返回到活动

时间:2017-07-19 18:24:07

标签: java android android-layout android-fragments

如何将片段1复制到活动? 我的 MainActivity 转到 - > 片段A / B 然后我想回到 MainActivity 。但是,如果我这样做通过onBackPressed()方法,那将关闭它的应用程序。我怎么解决呢?有办法解决吗?Thx

P.S。我的片段是整页而不是标签栏或列表。这是改变活动中的漏洞页面

这是我的代码:

    MainActivity:
    @Override
      public void onBackPressed() {
        if (getSupportFragmentManager().findFragmentByTag("Frag1") != null) {
          getSupportFragmentManager().popBackStackImmediate("Frag1",0);
        } else {

          super.onBackPressed();
        }
      }
    change fragments methods:
case 0:
        fragment = new webportal();

        bundle = new Bundle();
        bundle.putString("key", mLoginHost);
        fragment.setArguments(bundle);
        break;
      case 1:
        fragment = new keypaf();


          bundle = new Bundle();
        LcLoginClient.LcService service =
                mLoginClient.getService(PACKAGE_NAME_VOICE_CALL_SERVER);
        if (service != null) {
          serverPort = Integer.valueOf(service.get(LcLoginClient.LcService.Key.KEY_PORT));

          bundle.putString("key", mLoginHost);
          fragment.setArguments(bundle);
        }
        break;
 }
currentFragment = i;

    if (fragment != null) {
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
      ft.replace(R.id.fragment_container, fragment).addToBackStack("Frag1").commit();
    }

片段A片段B / c方法:

    public void keypad(View v) {


            Fragment fragment = new keypaf();
            Bundle bundle = new Bundle();

                bundle.putString(
                        host, mLoginHost);

                fragment.setArguments(bundle);
                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, fragment).addToBackStack("Frag1");
                fragmentTransaction.commit();
    }
public void remote(View v) {
        Fragment fragment = new Remote();
        Bundle bundle = new Bundle();
bundle.putString(
                        remote, mLoginHost);
            fragment.setArguments(bundle);
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment).addToBackStack("Frag1");

            fragmentTransaction.commit();
        }

    }

任何人都可以帮助我吗?

0 个答案:

没有答案