Search code examples
javaandroidandroid-fragmentsandroid-activitydesign-principles

Android design principles and the use of activities/fragments


I'm new with android apps development (but have some Java experience) and I am struggling a little bit with how I should design my app. For example:

When I execute the App I have a start page with the logo and two buttons: Register and LogIn. This should be the first activity.

1.) If I press the register button I see a page (another activity) with the input fields, a register button but also a Facebook and a Google+ Button.

2.) If I press the login button I see a page (another activity) with the input fields, a login button but also a Facebook and Google+ button.

Instead of implementing the facebook and google+ button twice, I thought about putting the google+ button and its functions into a seperate fragment and the same for the facebook button so I can reuse them.

Is this a "good" interpreatation of activities and fragments and if not when should I use fragments and activities? I thought about fragments like reuseable containers which can be implemented in different activities.

Thanks for any advice!


Solution

  • Activities, fragments and views have very similiar purpose, but on different levels. You can freely mix them as you wish as long as it's working for you. Personally I don't like fragments, so I'm only using activities and views in my apps. Here are the main differences:

    • Activities are the entry points. You can start your app using Intent to one of it's activities. You can't do that with other elements. You should use an activity when you plan an entry point. For example an email composing module which can be accessed by other apps.
    • Views are very light and simple. Use them to prepare reusable components, layouts and widgets. Views can be accessed by other apps only in a library form.
    • Fragments are somewhere in between. They consist of visual parts, data and application logic. Fragments can be used with backstack manager like activities, can't be launched using intents, can make use of layouts and widgets like views. Use fragments to create larger screens with backstack.

    And similarities:

    • All three mentioned elements can be displayed multiple at the same time. Activities using ActivityGroup, fragments using layouts and FragmentManager, views using layouts.
    • All three have their lifecycles. Fragments have the most complex lifecycle, views - the most simple.
    • All three can be used to compose applications. You can place layouts and widgets on screen using activities, fragments and views in a very similiar way.

    Basically activity consists of a window and a layout (and some data & logic). Fragment consists of a layout (and some data & logic). View is a layout or a widget (and some data & logic).

    Answering your question - This means that your approach is fine. At least for me. If you plan to reuse these buttons only as a UI components, you can rewrite them as views.