Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call parent's activity from a fragment

If I'm inside a Fragment how can I call a parent's activity?

like image 837
user1746708 Avatar asked Jan 16 '13 08:01

user1746708


People also ask

How can we call parent activity from fragment?

Simply call your parent activity using getActivity() method.

Can you call an activity from a fragment?

Best way of calling Activity from Fragment class is that make interface in Fragment and add onItemClick() method in that interface. Now implement it to your first activity and call second activity from there.

How do you call a method in a fragment?

Bookmark this question. Show activity on this post. I see in the Android Fragments Dev Guide that an "activity can call methods in a fragment by acquiring a reference to the Fragment from FragmentManager, using findFragmentById() or findFragmentByTag() ."

How do I call a fragment from activity in Kotlin?

This example demonstrates how to call an activity method from a fragment in an Android App using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

Yes, Its right by calling getActivity and cast it with parent activity to access its methods or variables ((ParentActivityName)getActivity())

Try this one.

ParentActivityName is parent class name

like image 191
DcodeChef Avatar answered Sep 28 '22 11:09

DcodeChef


2021 UPDATE

As it's told in the comments, Fragment#onAttach(Activity) is deprecated starting from API 23. Instead:

@Override public void onAttach(Context ctx) {     super.onAttach(ctx);          // This makes sure that the container activity has implemented     // the callback interface. If not, it throws an exception     try {         // Only attach if the caller context is actually an Activity         if (ctx instanceof Activity) {           mCallback = (OnHeadlineSelectedListener) ctx;         }     } catch (ClassCastException e) {           throw new ClassCastException(ctx.toString()                 + " must implement OnHeadlineSelectedListener");     } } 

ORIGINAL ANSWER

The most proper way is to make your Activity implement an Interface and use listeners. That way the Fragment isn't tied to any specific Activity keeping it reusable. Into the Fragment:

@Override public void onAttach(Activity activity) {     super.onAttach(activity);          // This makes sure that the container activity has implemented     // the callback interface. If not, it throws an exception     try {         mCallback = (OnHeadlineSelectedListener) activity;     } catch (ClassCastException e) {         throw new ClassCastException(activity.toString()                 + " must implement OnHeadlineSelectedListener");     } } 

That way, you make the Activity listen to the fragment when it's attached to it.

See also:

  • http://developer.android.com/training/basics/fragments/communicating.html
like image 21
Xtreme Biker Avatar answered Sep 28 '22 09:09

Xtreme Biker