Create generic/base form class regardless if activity or fragment

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Create generic/base form class regardless if activity or fragment



I'm trying to create a generic/base form regardless if it is an activity or fragment. To make it simple, a Form can submit so:


activity


fragment


Form


class BaseFormActivity extends AppCompatActivity {

public abstract void submitForm();

@Override
public void setContentView(int layoutResID) {
ConstraintLayout activityBaseForm = (ConstraintLayout) getLayoutInflater().inflate(R.layout.activity_base_form, null);
FrameLayout frameBaseForm = activityBaseForm.findViewById(R.id.frame_base_form);

getLayoutInflater().inflate(layoutResID, frameBaseForm, true);

findViewById(R.id.btn_submit).setOnClickListener(v -> submitForm()) // for the sake of simplicity, there's a button that will trigger submitForm() method

super.setContentView(activityBaseForm);
}
}



Here, I just include some default layout for a form, and a button for submit that triggers the abstract method submitForm(). But, this is only for android activities. How can I make this also available for fragments without writing a BaseFormFragment? I don't want to repeat default behaviors from activity to the fragment and vice versa.


submitForm()


activities


fragments


BaseFormFragment


activity


fragment









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

C# - How to create a semi transparent or blurred backcolor on windows form

Will Oldham

Makefile test if variable is not empty