Reputation: 928
I've basically got a dialogfragment which shows a range of options.
What i am trying to do is,
pass back to the calling activity, which option was selected. This will then, in the original activity call a method.
ATM I am using,
((Activity) method(); , to call the calling activities method from within the dialog, but this seems to be very in efficient as android response time slows down when doing this.
In other words, How can i simply, and quickly get information back from the fragment i just created?
In activity,
button.setOnClickListener( new OnClickListener() {
public void onClick(View arg0) {
FragmentManager fm = getFragmentManager();
FragClass frag = new FragClass();
frag.show(fm, "fragment_sub_connections");
}
and then in FragClass,
public FragClass()
{
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_sub_connections, container);
//some code here
}
Upvotes: 2
Views: 1282
Reputation: 76476
Create a Listener Interface and allow your activity to implement it, this can be used as a callback.
// In your fragment
public interface OnMyFragDismissListener(){
void onMyFragDismissed(MyObject obj);
}
// onCreate...
// someCode...
// However you have the user select the option
@Override
public void onDismiss(DialogInterface d){
if(getActivity() instanceof OnMyFragDismissListener)}
((OnMyFragDismissListener)getActivity()).onMyFragDismissed(result); // result being the user choice
}
}
Then use it like this:
public class YourActivity extends Activity implements OnMyFragDismissListener {
// code...
FragmentManager fragman = getFragmentManager();
FragClass frag = new FragClass();
frag.show(fm, "fragment_sub_connections");
// code...
@Override
public void onMyFragDismissed(MyObject obj){
// Fragment dismissed and object received!
}
}
Upvotes: 3