according to
http://developer.android.com/training/basics/fragments/communicating.html
The example described how to communicate with fragments via override the
onAttach in the fragment and try to casting the activity to the interface
we defined with a lot of hope that casting will work.
I think Fragment is kind of 'User Control' so when we create a new
instance, we determined explicitly if we want a certain action like we used
when we create a new button and we wanted this button to be clickable so we
adding the ability button.setOnClickListener(...)
i think this kind of implementation more intuitive for developers and i do
not force the activity to implement the interface.
So i suggest an implementation to communicate between the two Fragments
like we used to code
public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
@Override
public void setOnHeadlineSelectedListener(OnHeadlineSelectedListener
callback) {
mCallback = callback
}
...
}
and in the main activity or where you will use this fragment if you want
you can send your listener explicit
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
HeadlinesFragment firstFragment =
HeadlinesFragment.newInstance(getIntent().getExtras())
firstFragment.setOnHeadlineSelectedListener(...)//Depend how you
define to handle the listener 1-The activity will implement the interface
2-Annonymus function
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,
firstFragment).commit();
}
}
}
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/b4f46491-b127-4bb6-940b-c47114c4b31f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.