Reputation: 118762
I have the following handler:
public class SiteListener implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
}
}
I want to be able to call the following:
myActivity.getSupportFragmentManager()
.beginTransaction()
.add(R.id.site_info, siteInfoFragment)
.commit();
How do I gain access to the activity from SiteListener? Is there a way using the adapterView
?
Upvotes: 0
Views: 458
Reputation: 67286
You can cast FragmentActivity
with view
to get the instance of FragmentActivity
.
FragmentActivity activity = ((FragmentActivity)view.getContext());
activity.getSupportFragmentManager()
.beginTransaction()
.add(R.id.site_info, siteInfoFragment)
.commit();
Upvotes: 1
Reputation: 95578
Pass a reference to the activity to the SiteListener
when you create it.
EDIT Add code example:
public class SiteListener implements OnItemClickListener{
private MyActivity myActivity;
public SiteListener(MyActivity myActivity) {
this.myActivity = myActivity;
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
myActivity.getSupportFragmentManager()
.beginTransaction()
.add(R.id.site_info, siteInfoFragment)
.commit();
}
}
Upvotes: 3