How to access parent Activity View in Fragment
You can use
View view = getActivity().findViewById(R.id.viewid);
Quoting docs
Specifically, the fragment can access the Activity instance with
getActivity() and easily perform tasks such as find a view in the
activity layout
How to access a view of parent fragment from child fragment in navigation component
As i read your Explanation's You Know that ChildFragment Parent is NavHostFragment and NavHostFragment's Parent is ParentFragment.
NavHostFragment navHostFragment = (NavHostFragment) getParentFragment();
Fragment parent = (Fragment) navHostFragment.getParentFragment();
parent.getView().findViewById(R.id.element_id);
I have tried is my demo Project and work's for me.
How to access a particular View inside a Fragment from the parent Activity?
You can use findViewById()
on the Activity
as long as the object has an id. It will search the Activity
's entire view hierarchy, including attached child fragments, and is cleaner/easier to understand.
access parent fragment by child fragment
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.
How do I access a view from a Fragment from the Parent Activity?
You could just do this:
Interface:
public interface BtnClickable {
void myClickMethod(View v);
}
Fragment:
public class SomeFragment {
BtnClickable btnClickable;
//...onCreateView, etc.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_add_note, container, false);
Button btnGoToSignUp = root.findViewById(R.id.btnGoToSignUp);
btnGoToSignUp.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
btnClickable.myClickMethod(v);
}
});
}
public static void setClickListener(BtnClickable listener){
btnClickable=listener;
}
Activity:
class MyActivity implements BtnClickable {
//...onCreate etc instantiating your fragments
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.filters);
SomeFragment.setClickListener(this);
}
public void myClickMethod(View v) {
// you can listen here for btn clicked
}
}
Android : Acces parent fragment function from child fragment
Try this..
NavHostFragment navHostFragment = (NavHostFragment) getParentFragment();
ParentFragment parent = (ParentFragment) navHostFragment.getParentFragment();
parent.changeName()
Access Fragment viewPager in Parent Activity
add this code in your onBackPressed()
fragobjTab = (TabFragment) getSupportFragmentManager().findFragmentById(R.id.containerView);
try {
fragobjTab.onBackPressed();
}catch (Exception e){
e.printStackTrace();
}
And create a onBackPressed method in your tabFragment and setCurrentItem to the ViewPager:
public void onBackPressed()
{
// set page to your viewPager
// 0 is your first viewpager fragment
viewPager.setCurrentItem(0,true);
}
Access Parent Fragment method from Child Fragment
onCreateView
might be too early, try doing that onViewCreated
, or onStart
in the worst case.
Related Topics
Difference Between Surfaceview and View
Write Android Logcat Data to a File
Livedata Update on Object Field Change
How to Take Camera Capture Without a Preview from a Service or Thread
Make an Android Button Change Background on Click Through Xml
How to Get Screen Coordinates from Marker in Google Maps V2 Android
Centering Recyclerview Items with Flexboxlayoutmanager
How to Set Up Dagger Dependency Injection from Scratch in Android Project
Error: No Toolchains Found in the Ndk Toolchains Folder for Abi with Prefix: Llvm
Android + Pair Devices via Bluetooth Programmatically
Android - Configure Spinner to Use Array
Android: How to Change Checkbox Size
Open Soft Keyboard Programmatically
Android Runonuithread Explanation
How to Share a Sharedpreferences File Across Two Different Android Apps