jack asmack
jack asmack

Reputation: 9

why must a fragment accompany a different activity

ActivityA launches fragmentA. I need to launch fragmentB. There are 2 ways of doing it, but I dont know which is the best way First way, create a ActivityB which would launch FragmentB. Therefore, ActivityA--> ActivityB--> FragmentB.

The second way, is just replacing FragmentA with FragmentB

Fragment b = AddRecordFragment.init(Integer.valueOf(f.getId()));
getFragmentManager().beginTransaction().replace(R.id.container,b).addToBackStack(null).commit();

When I took the Udacity course, they perferred the first way, but I cant see the logic in creating an second activity (activityB) for the mere purpose of launching an Fragment. Can you tell me what is the advantages and disadvantages to each ?

Upvotes: 1

Views: 45

Answers (3)

NoChinDeluxe
NoChinDeluxe

Reputation: 3444

There really is no "right" way to do these kinds of things. There are recommended ways, there are best practices, there are common usages -- but all of this depends on what you, the developer, need to do to provide the best experience for your users and the best clarity and organization for fellow developers. Thinking through these things will give you the best answer for your particular scenario.

To address your particular question, think about what an Activity does, what its purpose is, and then think through why you would need Fragments. From the Android docs:

An Activity is an application component that provides a screen with which users can interact in order to do something...

For Fragments:

A Fragment represents a behavior or a portion of user interface in an Activity.

So already, even from the first sentence of each doc, we see a defined purpose for both components. An Activity is, well, an activity for the user. It's a screen with a purpose of allowing the user to DO SOMETHING. A Fragment, on the other hand, is a modular design that provides UI and other elements of interaction and data for the user that gets attached to an Activity. In other words, it helps the user do the thing that the Activity wants them to do, by either providing some point of interaction, or some visible form of data.

So now that we've clarified the purpose of these two components, ask yourself what the purpose of Activity B would be in your question. Will it provide the user with something new to do or see (even if through the use of Fragment B)? Will it provide something different from what the user was doing in Activity A? If so, then yes a new Activity is probably the best design choice. But if Fragment B is just another way of doing whatever it is the user is supposed to be doing in Activity A, or some sort of extension of that activity, then your best choice would be for Activity A to simply replace Fragment A with Fragment B itself through a FragmentTransaction.

Upvotes: 1

varunkr
varunkr

Reputation: 5542

It is not about the advantage, it is about what you want to do.

A Fragment represents a behavior or a portion of user interface in an Activity

Now it if you want the Fragment to be a portion of the first activity you launch it the second way. That is

Fragment b = AddRecordFragment.init(Integer.valueOf(f.getId()));
getFragmentManager().beginTransaction().replace(R.id.container,b).addToBackStack(null).commit();

But if you want it to be a portion of the second activity you launch it the first way.

Depends on which activity the fragment should be a part of.

Upvotes: 0

daxgirl
daxgirl

Reputation: 772

The entire idea of working with fragments is to create a flexible design. Meaning you can use same view container to display different content with different functionality. So, it's clear that if you need to display a different fragment, you should replace it in the container. In some cases you may prefer using a different activity, f.e. while launching settings, but if it's just a different content that can be contextualised with the same activity, definitely use fragment manager to replace in container.

Upvotes: 0

Related Questions