1

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!

Héctor
  • 24,444
  • 35
  • 132
  • 243
spcial
  • 1,529
  • 18
  • 40
  • 2
    I think you mean 'fragments' rather than segments? This question and answer is quite up to date and the link at the bottom of the question i particular might be worth looking at - there is no definitive answer but the discussion may help you decide what works for you: http://stackoverflow.com/q/20306091/334402 – Mick Jul 13 '15 at 08:19
  • 1
    Try Fragments try http://stackoverflow.com/questions/10478233/android-need-some-clarifications-of-fragments-vs-activities-and-views – TryinHard Jul 13 '15 at 08:36
  • Yes, sorry, i meant fragments. I've read the answers to the questions and as I see it is some kind of a reusable container? So in my example, would it make sense to put a button and its functionality (e.g. facebook login or google+ login) into a fragment or should I put everything in one activity? – spcial Jul 13 '15 at 08:57

1 Answers1

1

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.

Zielony
  • 16,239
  • 6
  • 34
  • 39
  • Thanks for the very useful answer. I think I was struggling about putting the buttons into fragments because fragments have actually more functionalities than i need (e.g. as you mentioned you can use the backstack manager like activities). Maybe this is already to heavy for a "simple" login button. But while these login buttons are implementing a facebook or google+ API for further functions I'm not sure if it is possible to use them in a View. Do you actually know if it is possible or should I just keep it as it is? – spcial Jul 13 '15 at 09:30
  • I don't know your coding style or your app, so it's hard for me to give you the exact answer. GLSurfaceView is probably the most complex one and it does a lot, so I bet that it's also possible in your case, the question is what is more convenient. – Zielony Jul 13 '15 at 09:42