Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access parent fragment by child fragment

this is my child fragment and how to access my parent fragment from this fragment please help me to fix it

public class Profilefragmant extends Fragment { public Profilefragment(){} @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,                          Bundle savedInstanceState) {      View rootView = inflater.inflate(R.layout.profile_layout, container, false);      TextView VE = (TextView) rootView.findViewById(R.id.profile_view_enquiry);     VE.setOnClickListener(new View.OnClickListener(){         public void onClick (View view){             Fragment fragment = null;              fragment = new Enquiryfragment();             replaceFragment(fragment);         }     });        return rootView; } public void replaceFragment(Fragment someFragment) {     android.support.v4.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();     transaction.replace(R.id.containerView, someFragment);     transaction.addToBackStack(null);     transaction.commit(); } } 

this is my error while run the project

 java.lang.IllegalArgumentException: No view found for id 0x7f0c0099 (com.knrmarine.knr:id/containerView) for fragment EnquiryActivity{3754711 #2 id=0x7f0c0099}  

This my Enquiryfragment code :

public class Enquiryfragmant extends Fragment { public Enquiryfragment(){} @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,                          Bundle savedInstanceState) {      View rootView = inflater.inflate(R.layout.enquiry_layout, container, false); return rootView;}} 
like image 343
Selva Avatar asked Mar 09 '17 12:03

Selva


People also ask

How can we call parent activity from fragment?

Simply call your parent activity using getActivity() method.


1 Answers

You can get parent Fragment's reference by :

ParentFragment parentFrag = ((ParentFragment)ChildFragment.this.getParentFragment()); parentFrag.someMethod(); 

I used this in my ViewPager. If you have other requirement, Interface or EventBus are two of the general ones.

like image 140
Kevin Murvie Avatar answered Oct 09 '22 21:10

Kevin Murvie