user3673449
user3673449

Reputation: 357

Inflate view in a single fragment

The app I'm currently developing has an action bar with 5 tabs (fragments). One of these fragment shows an alert dialog, but the layout is blank. I want to put a background image, so I created a layout for that fragment and used inflater.inflate(...) method to set the layout.

Problem is that line of code sets that layout to ALL fragments. How can I limit it just to the fragment I need? Here's my code:

public class MyFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        //...
        AlertDialog ad = new AlertDialog.Builder(getActivity()).setTitle("Title")
//...

        ad.show();
        inflater.inflate(R.layout.fragment_my, container); //this is the layout I want to inflate to my fragment
        return super.onCreateView(inflater, container, savedInstanceState);
}

Also tried to replace the last two lines with:

return inflater.inflate(R.layout.fragment_trovami, container);

but i'm getting this error:

07-27 16:19:46.768: E/AndroidRuntime(1998): java.lang.IllegalStateException: 
The specified child already has a parent. You must call removeView() on the child's parent first.

Upvotes: 0

Views: 282

Answers (1)

Itzik Samara
Itzik Samara

Reputation: 2288

Answer :

return inflater.inflate(R.layout.fragment_trovami, container,false);

Upvotes: 1

Related Questions