Skip to content

Instantly share code, notes, and snippets.

@blackcj
Created August 15, 2013 19:53
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save blackcj/6244204 to your computer and use it in GitHub Desktop.
Save blackcj/6244204 to your computer and use it in GitHub Desktop.
Demonstrates how to call a function in the parent Activity from within a Fragment.

Step 1: Add any functions you want to call into the interface (EventListener).

Step 2: Implement those functions in your MainActivity.

Step 3: Create the listener in your Fragment and attach it to the Activity.

Step 4: Call any functions on the listener.

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by cblack on 8/15/13.
*/
public class DetailFragment extends Fragment {
private EventListener listener;
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
if(activity instanceof EventListener) {
listener = (EventListener)activity;
} else {
// Throw an error!
}
}
/**
*
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_detail, container, false);
listener.sendDataToActivity("Hello World!");
return v;
}
}
/**
* Created by cblack on 8/15/13.
*/
public interface EventListener {
public void sendDataToActivity(String data);
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends FragmentActivity implements EventListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.x_activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void sendDataToActivity(String data) {
Log.i("MainActivity", "sendDataToActivity: " + data);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:name="com.example.listenerdemo.DetailFragment"
android:id="@+id/detail_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
@hieuden0
Copy link

how about call a function in the parent Frament from within a Activity.

Thanks :((

@Qleoz12
Copy link

Qleoz12 commented Jun 22, 2018

mmm i used this code
android.app.Fragment tt=getFragmentManager().findFragmentById(R.id.detail_fragment);
DetailFragment tx=(DetailFragment) tt;
// and now can you call methos from inside the class DetailFragment->event listener

@coolquad
Copy link

I want to call Method (function) of Fragment from Parent Activity.
Can you explain more about this?
Your code looks like exchange data from fragment to Activity.

@dedolence
Copy link

I want to shake your hand! Hours spent searching how to do this. I'm new to the language and people don't often realize how much context they're leaving out when they offer solutions, so I tried this style of implementation before but it never worked. It wasn't until I saw your clear, complete code that it clicked and it works for me. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment