I create a view pager in my application and use a class that extends the fragment on it. When I create an instance, I can transfer all the elements (image, text, etc.) and save it using the Bundle to use it in onCreate. But I can not store the button listener in the fragment. Here is my class:
public class RegWizardFragmentInfo extends Fragment { private static final String IMAGE = "image"; private static final String TEXT = "text"; private static final String BUTTON = "buttonText"; private View.OnClickListener buttonCallBack; private Button button; private int image; private int text; private int buttonText; public RegWizardFragmentInfo newInstance(int image, int text, int buttonText, View.OnClickListener callback) { RegWizardFragmentInfo fragment = new RegWizardFragmentInfo(); Bundle bundle = new Bundle(); bundle.putInt(IMAGE, image); bundle.putInt(BUTTON, buttonText); bundle.putInt(TEXT, text); fragment.setArguments(bundle); fragment.setRetainInstance(true); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.onActivityCreated(savedInstanceState); this.image = getArguments().getInt(IMAGE); this.text = (getArguments() != null) ? getArguments().getInt(TEXT) : -1; this.buttonText = (getArguments() != null) ? getArguments().getInt(BUTTON) : -1; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ViewGroup rootView = (ViewGroup) inflater.inflate( R.layout.fragment, container, false);
So, how can I save the listener that I get in newInstance to add it to the button onCreateView method?
Thanks for the help.
source share