Abhishek Bhardwaj
Abhishek Bhardwaj

Reputation: 1204

Communication between two fragment from different activity?

I have two activities in my app. In MainActivity I am loading 2 fragments Fragment1 and Fragment2. From Fragment1 i am jumping to Fragment2 and from Fragment2 I am jumping to Activity2 which contains Fragment2_1 From Fragment 2_1 I want to jump back to Fragment2 in MainActivity but I am getting an error when I try to do this.

I want to know is it possible to jump from 1 activity fragment to another activity fragment?

My code is:

MainActivity.java

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();


    }
}

Fragment1.java:

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;
    }

Fragment2.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;

    }

}

Activity2.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_1.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;
    }

}

All code works fine but when I want to move from Fragment2_1 to Fragment1 my app crashes with an exception :

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

Upvotes: 0

Views: 313

Answers (2)

rafsanahmad007
rafsanahmad007

Reputation: 23881

try this: in 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);
                }
            });

Now in Mainactivity onResume() load your desired fragment in maincontainer

in 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;
  }
}

Upvotes: 1

Abhishek Jain
Abhishek Jain

Reputation: 3702

In your Fragment2_1 within your second activity you are trying to place Fragment2 in the container R.id.mainContainer. Your code below :

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

But since you are currently inside your second activity that container doesn't exist (it is part of your first activity). You need to either load your your MainActivity again or finish Activity2 by calling finish(). If you do the later android will by itself take you back to your MainActivity and since you moved to Activity2 from Fragment2 android will also remember that and show you Fragment2.

Your Fragment2_1 should look like this:

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;
    }

}

OR, If you just want to show Fragment2 in your second activity you can do something like this :

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 is the container where you need to replace the fragment.

Note that : You will still be in your second activity. Just the fragment will be replaced.

Upvotes: 0

Related Questions