Code example for Fragment

Methods: setArguments

0
    public static Fragment newInstance(String text, int id) {
        final Fragment result = new TextButtonFragment();
        final Bundle args = new Bundle();
        args.putString(KEY_STR, text);
        args.putInt(KEY_ID, id);
        result.setArguments(args);
        return result;
    } 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if ( getArguments() != null ) { 
            mTag = getArguments().getString(KEY_STR);
            mId = getArguments().getInt(KEY_ID);
        } else { 
            mTag = "U";
            mId= -100;
        } 
    } 
 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_button, null);
        mEdit = (EditText) view.findViewById(R.id.edit);
        final Button btn = (Button) view.findViewById(R.id.btnStart);
        btn.setOnClickListener(this);
        btn.setText(mTag+"."+mId);
        return view;
    } 
    @Override 
    public void onClick(View v) {
        final Fragment fragment = newInstance(mTag, ++mId);
        ((BaseActivity<?,?>)getActivity()).getBackStack().startFragmentAt(fragment, null);
    } 
} 
Experience pair programming with AI  Get Codota for Java