2

I'm building an android app where there is a button that will show a new screen. Should I use Fragment and FrameLayout, or create a new Activity?

I wanted to replicate the behaviour of the FaceBook app, where the user clicks a button, which then shows a new screen where the user will type what he/she wants to write

  • you can use activity for new screen. – Jamil Aug 05 '14 at 13:51
  • I'd use Fragments everywhere I can, because they are more stable. Like in this here I posted: http://stackoverflow.com/questions/24840509/why-does-the-new-adt-create-a-static-inner-class-fragment-by-default-simple-fr – EpicPandaForce Aug 05 '14 at 13:55

4 Answers4

2

Fragments are re-usable components. You can swap between fragments inside an Activity.

If you are going in to a new screen, best would be to use an Activity.

DanKodi
  • 3,550
  • 27
  • 26
2

You can use either Activity approach or Fragment approach. The decision should be based on what you want to do int the next screen. Over the years I find that using fragment is suitable for most of the scenarios. I prefer fragment because of its flexibility and re-usability(also no need to add activity tag in manifest one of my fav benifit lol).

Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
  • 1
    +1 because I run into "NoActivityFoundException, are you sure you've specified the Activity in the AndroidManifest.xml?" all the damn time. Even if you know you need to add it, I personally tend to forget about it. – EpicPandaForce Aug 05 '14 at 13:56
  • 1
    @Zhuinden that is one of the most annoying exceptions that android throws. Wastes 30 seconds for 1 in every three new activity I create and forget to add to manifest. – Illegal Argument Aug 05 '14 at 13:58
  • @IllegalArgument Thanks, I'll try and use Fragments whenever possible then. –  Aug 05 '14 at 14:09
2

As I posted in Why using Fragments? previously, due to how Activities are destroyed and reconstructed on every single orientation change, I'd rather keep my things in Fragments for stability, and use Activities only where necessary.

So personally, I would recommend this setup for a start: https://codereview.stackexchange.com/questions/57543/why-does-the-new-adt-create-a-static-inner-class-fragment-by-default

Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
1

You havent really provided alot of information about what you would use it for. I would use an activity if it a competely new screen. Fragments, layouts and views etc are really things that an activity can have in them. If you wanted to just have a popup style view then you could use differnet views within the activity.

Rob85
  • 1,719
  • 1
  • 23
  • 46
  • I wanted to replicate the behaviour of the FaceBook app, where the user clicks a button, which then shows a new screen where the user will type what he/she wants to write –  Aug 05 '14 at 14:00
  • Honestly for that I'd just use a DialogFragment. – EpicPandaForce Aug 05 '14 at 14:02
  • if you are going to just display a kind of popup style entry box that will then copy the contense back to the orignal activity then use fragments, see @illegal Argument's comments above – Rob85 Aug 05 '14 at 14:04
  • @Zhuinden Thanks, I think DialogFragment is exactly what I need –  Aug 05 '14 at 14:08
  • I personally did http://stackoverflow.com/a/25056160/2413303 for a DialogFragment, it is also worth noting how to make it fill the parent if you'll need it, http://stackoverflow.com/questions/23990726/how-to-make-dialogfragment-width-to-fill-parent – EpicPandaForce Aug 05 '14 at 14:11