SalamBoy
SalamBoy

Reputation: 58

how to start a fragment from the activity

I have a activity , and i have a 2 fragments
When i move from fragment about a fragment , i use :

class a extends activity{
.
.
.
public void onClick(View v) {
                FragmentTransaction trans = getFragmentManager().beginTransaction();
                trans.replace(R.id.frag, new Mobile());
                trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                trans.addToBackStack(null);

        trans.commit();
   }
.
.
.
.
}

the code of fragment :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    /* pour creer le fragment */

    view = inflater.inflate(R.layout.rechercher, container, false);
    return view;
}

how to start a fragment from the activity ? Note : I can not define getFragmentManager() in activity !!

Upvotes: 0

Views: 105

Answers (1)

Anıl
Anıl

Reputation: 136

It's probably because you don't have the activity context in the method onClick. Try to use :

getApplicationContext().getFragmentManager().beginTransaction();

Upvotes: 1

Related Questions