Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Weather App (2015) Concurrency and Error Handling What To Do When the Network is Down

Jeremiah Shore
Jeremiah Shore
31,168 Points

How do I avoid using "non-default constructors in fragments"?

I added a non-default constructor to my AlertDialogFragment class so that I can pass in the strings that will be used for the dialog. This constructor then sets the member variables, which are used in the builder.set[property] methods.

public AlertDialogFragment(String errorTitle, String errorMessage, 
    String errorButtonText){
        mErrorTitle = errorTitle;
        mErrorMessage = errorMessage;
        mErrorButtonText = errorButtonText;
}

I am getting a warning from Android studio: "Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead".

What does this mean and what would be the "proper" way to accomplish what I want to do?

2 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Jeremiah;

Take a look at this post over at StackOverflow. There are some great comments and examples there.

Post back with further questions.

Ken

Jeremiah Shore
Jeremiah Shore
31,168 Points

Thanks for the direction. Looks like the most sensible answer on there is the on that starts with "Your Fragment shouldn't have constructors because of how the FragmentManager instantiates it. You should have a newInstance() static method defined and pass any parameters via arguments (bundle)", even if it isn't top voted.

Here is my re-factored code for anyone who may happen upon this question:

public class AlertDialogFragment extends DialogFragment {

    public static final AlertDialogFragment newInstance(String title, String message, String buttonText) {
        AlertDialogFragment adf =  new AlertDialogFragment();
        Bundle bundle = new Bundle(3);
        bundle.putString("title", title);
        bundle.putString("message", message);
        bundle.putString("buttonText", buttonText);
        adf.setArguments(bundle);
        return adf;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        String errorTitle = getArguments().getString("title");
        String errorMessage = getArguments().getString("message");
        String errorButtonText = getArguments().getString("buttonText");
        Context context = getActivity();
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(errorTitle)
                .setMessage(errorMessage)
                .setPositiveButton(errorButtonText, null);
        return builder.create();
    }
}

Here is an example of using the .newInstance method to create a new AlertDialogFragment:

private void showNetworkErrorDialog() {
        AlertDialogFragment dialog = AlertDialogFragment.newInstance(
                getString(R.string.network_error_title),
                getString(R.string.network_error_message),
                getString(R.string.error_ok_button_text));
        dialog.show(getFragmentManager(), "network_error_message");
    }
Nathan Beauchamp
Nathan Beauchamp
2,456 Points

So, if you use this way to customize the text in the alert dialog fragment, will you have to use the .newInstance() method every time you call it? Including our method in MainActivity alertUserAboutError()?

Ivan Sued
Ivan Sued
5,969 Points

Jeremiah and Ken,

Thanks for the resource. I was having issues with this as well. I thought the bundle was supposed to be created in the main activity you were working with. The whole instance is what I missed. Thanks again.

Ashley Ewen
PLUS
Ashley Ewen
Courses Plus Student 1,194 Points

Thanks for this, I was literally having the same issue trying to reuse a alert class and wasn't sure what was going wrong.