Created
August 15, 2013 19:53
-
-
Save blackcj/6244204 to your computer and use it in GitHub Desktop.
Revisions
-
blackcj revised this gist
Aug 15, 2013 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. -
blackcj revised this gist
Aug 15, 2013 . 3 changed files with 1 addition and 1 deletion.There are no files selected for viewing
File renamed without changes.This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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.x_activity_main); } File renamed without changes. -
blackcj created this gist
Aug 15, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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>