Nixit Patel
Nixit Patel

Reputation: 4445

Fragment best practice - Android

I am struggling with the structure of the fragment, Thing is... In Activity there is two fragments. One contains a list. Call this FragmentA. The other contains detail. Call this FragmentB.

With every list item in FragmentA there is a different view for FragmentB, so what is the preferred way to handle this kind of scenario?

Thank You

Upvotes: 3

Views: 2477

Answers (1)

Jonathan Schneider
Jonathan Schneider

Reputation: 27727

Without seeing the complexity of the app in question, I would suggest that each different view for FragmentB be represented in its own fragment.

The use the Fragment Transaction method to replace the placeholder (let's call this R.id.fragment_container) where FragmentB is with the appropriate fragment depending on your selection in FragmentA. Something like this:

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

Upvotes: 3

Related Questions