Danny Delott
Danny Delott

Reputation: 6988

NavigationDrawer with Fragments or Activities? Also, please school me on proper Fragment management

My app uses the ActionBarCompat and NavigationDrawer, which begs the question of how is one supposed to structure the navigation? I see two options:

1) Use a single Activity and swap out Fragments.

2) Use multiple Activities, and transition between them.

The latter seems to be the correct way, however this means that each Activity must get and build the ActionBar and NavigationDrawer when called from another Activity.

How will this affect my transition animations? If I have a left-to-right transition effect when a navigation item is clicked, will it transition the entire Activity or everything below the ActionBar?

The former seems fraught with issues. A single .java class file could get really heavy and difficult to manage.

But while we're on the subject of Fragments, please consider this situation and school me on proper Fragment management:

A blank Activity with just a LinearLayout attaches a new Fragment to it that contains a Button. Inside the Fragment class, this button gets an onClickListener and when the button is clicked, another of the same kind of Fragment should be attached to the LinearLayout inside the Activity.

A) Should the Fragment class communicate to the Activity that a new Fragment should be attached? This could be done with a custom public interface and the callback being executed within the onClickListener in the Fragment class.

B) Or, should the Fragment handle attaching the new instance inside the onClickListener, making the Activity work less? By simply bridging the clicked Button and calling .getParent() until the LinearLayout is reached, then attaching it there.

Upvotes: 1

Views: 81

Answers (1)

Zyoo
Zyoo

Reputation: 773

A blank Activity with just a LinearLayout attaches a new Fragment to it that contains a Button. Inside the Fragment class, this button gets an onClickListener and when the button is clicked, another of the same kind of Fragment should be attached to the LinearLayout inside the Activity.

A) Should the Fragment class communicate to the Activity that a new Fragment should be attached? This could be done with a custom public interface and the callback being executed within the onClickListener in the Fragment class.

B) Or, should the Fragment handle attaching the new instance inside the onClickListener, making the Activity work less? By simply bridging the clicked Button and calling .getParent() until the LinearLayout is reached, then attaching it there.

You could just call FragmentManager and make replace transaction from the fragment itself.

FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(container_id, newFragment, tag);
transaction.commit();

There's no need to communicate with Activity because the Activity has FragmentManager. http://developer.android.com/guide/components/fragments.html#Transactions

My app uses the ActionBarCompat and NavigationDrawer, which begs the question of how is one supposed to structure the navigation? I see two options:

1) Use a single Activity and swap out Fragments.

2) Use multiple Activities, and transition between them.

The latter seems to be the correct way, however this means that each Activity must get and build the ActionBar and NavigationDrawer when called from another Activity.

It's okay with which way you choose, as long as you have the same NavigationDrawer in each Activity/Fragment.

How will this affect my transition animations? If I have a left-to-right transition effect when a navigation item is clicked, will it transition the entire Activity or everything below the ActionBar?

It will do transition to the whole screen if you change Activity, below ActionBar if you use Fragment. And don't forget to dismiss the NavigationDrawer if you swap Fragment.

Upvotes: 1

Related Questions