Frank Brosnan
Frank Brosnan

Reputation: 231

Where to place application logic in new Activity code generated by ADT 22.6.2

In the latest versions of eclipse (ADT v22.6.2) the create android application now generates an activity_main.xml and a fragment_main.xml. It generates only a single activity class but this now has an embedded inner static fragment class that is created by the activity in its onCreate method

@Override
protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new       PlaceholderFragment()).commit();
    }

....
public static class PlaceholderFragment extends Fragment

My confusion is how to port old code/examples where there was only 1 activity and the main application logic is usually put in the Activity onCreate method i.e. stuff like findViewById. Listeners etc

The way I have approached it is that I put all my user created views as members of the static PlaceHolderFragment class. Then I call this in the fragment onCreateView. I still have some logic in the activity and it stores a pointer to the fragment. It updates the views members by calling getters on the fragment. Is this correct or should all logic be moved to the fragment now ? All the tutorials/examples use the old approach where logic is placed in the activity so there is no reference documentation for how to use the new files that Eclipse generates for an Android application. Any help appreciated ?

Upvotes: 2

Views: 871

Answers (1)

Juan Pedro Martinez
Juan Pedro Martinez

Reputation: 1974

Don't worry about the files that Eclipse generate automatically: You can do whatever that you want!!!

Fragment is a element between an Activity and a container.That's mean that you can put the logic of your code inside of one fragment with not problems.

In theory, fragments are used when you want to manage screens using different modules, containers. It's a fragment, a module, part of one screen (but also can be used in a full screen looking as an activity and with the same behaviour than one activity.) For example, imagine that you have for mobile phone screens, one list of news in one screen, and when you click, your app go to the next screen for show the content of the news, right? Ok, so you can use for these 2 screens: 2 activities for each one or one parent activity with 2 fragments...whatever that you want...

Imagine the same case, for a tablet version of your app, now the left part of the screen you should show the list of news and in the right part of the screen, the contain of each news clicked, right? In that case, would be completly necessary to use one activity parent with two fragments...in that case, we could reuse almost the same case for the mobile phones or tablet.

And now, focus in your question: if you don't want complicate the life (not too much, because work with fragment is easy too) I will recomend you to use only activities for your app.

But, like your question, you want to port, there isn't any problem. Now imagine that your activity is going to be only the class where you are going to manage the fragments. The logic for each screen has to be in each fragment. Use the activity only for replace fragments, or share information between fragments, etc. The activity will be like the orchestra director.

From each fragment, you can access to methods or public variables of your activity using ((NameOfActivity)getActivity()).

Is it clear for you?

One more stuff, in fragment, normally the method that we used for initialize stuffs is onCreateView (and not onCreate like activities).

Upvotes: 2

Related Questions