不同活动的两个片段之间的通信?

时间:2017-01-28 07:22:51

标签: android android-studio android-fragments

我的应用中有两项活动。在Fragment1我正在加载2个片段Fragment2Fragment1。从Fragment2我跳到Fragment2Activity2我跳到Fragment2_1,其中包含Fragment 2_1来自Fragment2我想跳回到MainActivity public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction(); Fragment fragment1=new Fragment1(); fragmentTransaction.replace(R.id.mainContainer,fragment1,"MainActivity"); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } } public class Fragment1 extends Fragment { public Fragment1() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view=inflater.inflate(R.layout.fragment_fragment1, container, false); Button buttonfrag1=(Button)view.findViewById(R.id.buttonfrag1); buttonfrag1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { FragmentManager fragmentManager=getActivity().getSupportFragmentManager(); FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); Fragment2 fragment2 = new Fragment2(); fragmentTransaction.replace(R.id.mainContainer,fragment2,"Fragment1"); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } }); return view; } 但我尝试这样做时遇到错误。

我想知道是否可以从1个活动片段跳转到另一个活动片段?

我的代码是:

MainActivity.java

public class Fragment2 extends Fragment {


    public Fragment2() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_fragment2, container, false);
        Button buttonfrag2=(Button)view.findViewById(R.id.buttonfrag2);
        buttonfrag2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(),Activity2.class);
                startActivity(intent);

            }
        });

        return view;

    }

}

Fragment1.java:

public class Activity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);
        FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
        Fragment2_1 fragment2_1=new Fragment2_1();
        fragmentTransaction.replace(R.id.frag2_1,fragment2_1,"Activity2");
        fragmentTransaction.commit();


    }
}

Fragment2.java

public class Fragment2_1 extends Fragment {


    public Fragment2_1() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view= inflater.inflate(R.layout.fragment_fragment2_1, container, false);
        Button buttonfrag2_1=(Button)view.findViewById(R.id.buttonfrag2_1);
        buttonfrag2_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction();
                Fragment2 fragment2=new Fragment2();
                fragmentTransaction.replace(R.id.mainContainer,fragment2,"frag2_1");
                fragmentTransaction.addToBackStack(null);

                fragmentTransaction.commit();
            }
        });

        return view;
    }

}

Activity2.java

java.lang.IllegalArgumentException: No view found for id 0x7f0b0059 (saleskit.orbitsys.com.fragmentdemo:id/mainContainer) for fragment Fragment2{29eb9c90 #1 id=0x7f0b0059 frag2_1}

Fragment2_1.java

{{1}}

所有代码都运行正常但是当我想从Fragment2_1移动到Fragment1时,我的应用程序崩溃并出现异常:

{{1}}

2 个答案:

答案 0 :(得分:1)

试试这个:在Fragment2_1中

 buttonfrag2_1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   Intent intent = new Intent(getActivity(), MainActivity.class);
                    intent.putExtra("frag", "fragment2");
                    startActivity(intent);
                }
            });

现在进入主动态onResume()maincontainer

中加载您想要的片段 MainActivity

中的

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

  Intent intent = getIntent();

if(intent.getExtras() != null)
{
 String frag = intent.getStringExtra("frag");
}


  switch(frag){

    case "fragment2":
       //here you can load Fragment2 to your activity as usual;
        fragmentManager.beginTransaction().replace(R.id.mainContainer, new Fragment2()).commit();
       break;
  }
}

答案 1 :(得分:0)

在第二项活动的Fragment2_1中,您尝试将Fragment2放入容器R.id.mainContainer。您的代码如下:

FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction();
Fragment2 fragment2=new Fragment2();
fragmentTransaction.replace(R.id.mainContainer,fragment2,"frag2_1");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

但是,由于您目前在第二个活动中,容器不存在(它是您第一个活动的一部分)。您需要再次加载MainActivity或通过调用Activity2完成finish()。如果你这样做,后来的android会自动带你回到你的MainActivity,因为你从Activity2移动到Fragment2,android也会记住这个并向你显示Fragment2。< / p>

你的Fragment2_1应该是这样的:

public class Fragment2_1 extends Fragment {

    public Fragment2_1() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view= inflater.inflate(R.layout.fragment_fragment2_1, container, false);
        Button buttonfrag2_1=(Button)view.findViewById(R.id.buttonfrag2_1);
        buttonfrag2_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               getActivity().finish();
            }
        });

        return view;
    }

}

,如果您只想在第二项活动中展示Fragment2,可以执行以下操作:

public class Fragment2_1 extends Fragment {


    public Fragment2_1() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view= inflater.inflate(R.layout.fragment_fragment2_1, container, false);
        Button buttonfrag2_1=(Button)view.findViewById(R.id.buttonfrag2_1);
        buttonfrag2_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction();
                Fragment2 fragment2=new Fragment2();
                fragmentTransaction.replace(R.id.frag2_1,fragment2,"frag2");
                fragmentTransaction.addToBackStack(null);

                fragmentTransaction.commit();
            }
        });

        return view;
    }

}

R.id.frag2_1 是您需要替换片段的容器。

请注意:您仍将处于第二项活动中。只是片段将被替换。