goodm
goodm

Reputation: 7295

Everything on one Activity - Android

How do you think? I think to create small app and put everything on only one Activity. Just change the content Views. Get rid off problems with share data, start from recent apps and couple more. I know that in that way I can get much more problems, like stackoverflow. But did someone try it?

Upvotes: 1

Views: 421

Answers (6)

raider33
raider33

Reputation: 1673

I agree with all of positives of using Activities and Fragments. My card-game app was initially written with 1 activity and 7 views. Eventually I refactored the application to use 7 Activities. The start up time of the application dropped significantly. However, when using 7 views, the app never had memory problems, and once loaded, was lightening fast between screens.

So my conclusion is that using views vs. activities (or fragments) is more of a trade-off than the slam-dunk presented by other answers. Views take longer to initially load, but are super-quick to swap. Activities are modular and are only loaded when needed. Memory is less and load time is quicker when using Activities, but the trade off is more complicated code and slower transitions between screens.

Finally, you don't have to use an all or nothing approach. You can always mix both solutions into your application, which is what I finally settled. I use Activities for screens that are infrequently used and Views/ViewSwitcher for the main 2-screens that are frequently used. Its the best of both worlds.

Upvotes: 0

Nigel
Nigel

Reputation: 161

If its the kind of project you can abandon after handover then you can get away with using one class

If however, your client is long term and will be requesting future changes and or extensions then it best not to dump everything in one class. Changes are hard to anticipate and you may find yourself refactoring everything if its all in one activity.

Upvotes: 0

Android
Android

Reputation: 3868

I would suggest you to use fragments. These fragments can be called through one activity. Although I am sure of that simple one activity will do, because I used one activity class for handling fragments, but i was using few more activity class also.

For ex. You can create different fragments and call them in activity like;---

 public void onStatusClick(View view)
    {
        setFragment(new HomeFragment());
    }

    public void onNotificationsClick(View view)
    {
        setFragment(new NotificationsFragment());
    }


    public void onContactClick(View view)
    {
        setFragment(new ContactFragment());
    }

    public void setFragment(Fragment fragment)
    {
        FrameLayout framelayout = (FrameLayout) findViewById(R.id.frme_container);

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(framelayout.getId(), fragment).commit();

    }   

Upvotes: 0

Ewoks
Ewoks

Reputation: 12435

My friend and I were making 1st application together and he insisted to make it like that..It was laggy, unstable and it can be characterized as "the worst practice" example..

I don't agree that it runs much faster, I agree that you need to do additional programming, which Android by default would handle automatically if u follow good practice (like BACK pressed) and I am sure that it will lead to a lot of memory leaks, unnecessary memory use, and decrease of performance. All in all it doesn't leave good impression to someone who is using it..

If you are doing it just to avoid sharing data and other "problems" it is much better to invest some time in this topics and try to learn more about them, to start using them in a proper way. When you know framework a bit better you will understand why such ideas of putting everything in one activity is bad.

moral I learned:

  1. It is terrible idea to put everything in one activity, if your application in reality needs more components !!! Blockquote

  2. Choose your programming companion wisely!

  3. Learn Learn Learn..

good luck ;)

Upvotes: 3

Egor
Egor

Reputation: 40193

Yes, a couple of my first apps were made using exactly this approach. There are some advantages like:

  1. It usually works a bit faster than changing from one activity to another.
  2. You have just one Activity class.

and some disadvantages:

  1. You may confuse yourself if you have too many screens.
  2. You must code the screen changing logic yourself.
  3. This is not considered as usual practice for Android development.

Anyway, this approach works, so it's your decision to use it or not. Hope this helps.

Upvotes: 1

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

Yes... it's definitely doable. However, it's a really terrible idea for anything other than the most basic apps. I had the same impulse when I was first starting out and built an app that had 5 screens. It quickly became much more trouble than it was worth, and that one activity had SO much code, it was a nightmare.

If you're targeting Honeycomb and > you should just use fragments. It's much nicer.

If your primary motivation for doing this is the complexity of passing data around from Activity to Activity you should consider extending Application and just storing all your persistant data (by that I mean the stuff you need while the app is actually running, not when it's backgrounded) up there.

If you're not doing any substantial interacting with the user and just want to swap out XML views with minimal functionality, you could be ok with the one Activity approach, but for most applications it's just too messy.

Upvotes: 2

Related Questions