Skip to content

Instantly share code, notes, and snippets.

@blackcj
Created August 15, 2013 19:53
Show Gist options
  • Select an option

  • Save blackcj/6244204 to your computer and use it in GitHub Desktop.

Select an option

Save blackcj/6244204 to your computer and use it in GitHub Desktop.

Revisions

  1. blackcj revised this gist Aug 15, 2013. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions ActivityEventListener.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    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.
  2. blackcj revised this gist Aug 15, 2013. 3 changed files with 1 addition and 1 deletion.
    File renamed without changes.
    2 changes: 1 addition & 1 deletion MainActivity.java
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ public class MainActivity extends FragmentActivity implements EventListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setContentView(R.layout.x_activity_main);
    }


    File renamed without changes.
  3. blackcj created this gist Aug 15, 2013.
    37 changes: 37 additions & 0 deletions DetailFragment.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    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;
    }
    }
    8 changes: 8 additions & 0 deletions EventListener.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    /**
    * Created by cblack on 8/15/13.
    */
    public interface EventListener {

    public void sendDataToActivity(String data);

    }
    27 changes: 27 additions & 0 deletions MainActivity.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    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.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);
    }
    }
    4 changes: 4 additions & 0 deletions _ActivityEventListener.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    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.
    13 changes: 13 additions & 0 deletions activity_main.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    <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>
    7 changes: 7 additions & 0 deletions fragment_detail.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    <?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>