What Is the Differences Between onAttach() and isAdded() in Android Fragment?

Can anyone tell me what are the different of use of onAttach() and isAdded in Android Fragment?

When create a new fragment onAttach() method creating default.

I checked this question, the question is asked about !onDetech() and isAdded() , also one answer there, but I am not clear with that.. Thanks

1

3 Answers

This could be answered simply reading javadocs:

    /**
     * Called when a fragment is first attached to its context.
     * {@link #onCreate(Bundle)} will be called after this.
     */
    public void onAttach(Context context) 

and

    /**
     * Return true if the fragment is currently added to its activity.
     */
    final public boolean isAdded()

Called when a fragment is first attached to its context. onCreate(Bundle) will be called after this.

Return true if the fragment is currently added to its activity.

onAttach() is called when a fragment is connected to an activity. Where isAdded() override method will come up with true if it is connected successfully to an activity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest