Android: What is better - multiple activities or switching views manually?

I have developed some apps for Android, and this questions stays always:

How should I structure my UI? Should I launch activity after activity and leave the phone to make the "back" button, or should I choose more optimized, but more complex to implement, way with switching manually Views and then manually doing the "Back" button functionality?

What do you think (or know) is the better practice?


Asked by: Miller330 | Posted: 24-01-2022






Answer 1

I would say that multiple Activities almost always makes more sense. I just don't think Android is designed for constantly switching its own views - you miss out on so much. You have to implement Back yourself, you don't get any inter-Activity transitions, you have to implement a lot of internal logic to resume an application in the correct state. If you don't partition your app into Activities, it makes it a lot more difficult later on to change the flow of your application. It also results in one mega-Activity that can be a lot harder to handle than a lot of smaller pieces of code.

I have trouble imagining that speed is really an issue; if it is then there's something wrong with the way you're initializing each Activity. For example, I used try to pass Serializable objects between Activities, and that proved to be incredibly slow; when I switched to a faster method of passing objects, the speed of launching Activities increased immensely.

Also, I think it's telling that the Android guidelines for Activity and Task Design don't mention switching Views at all; it's centered around an Activity-as-View design.

Answered by: Elian555 | Posted: 25-02-2022



Answer 2

I'd like to point out some instances when a single activity might be better design for an Android application that has more than one full screen View:

  • If the application screens are tightly coupled and share a common Object that they are all operating on. In this case passing around the Object may require a Bundle and can be error prone since there will be copies of it. A good example might be a wizard. Yes you could use static's to access the common Object but static can be dangerous in Android (think configuration changes!)

  • If you want some really cool animations in between screens. Maybe you want a bird to take off in one screen and land in another screen. Try doing that when each screen is an activity!

On the other hand if one of your screens is designed to be shown by any number of other applications then that screen should be its own Activity.

UPDATE March 2014:

At this point the question should now include the choice of Fragments. I think that Views are probably the least likely choice of the 3: Activity, Fragment, View. If you want to implement screens that make use of the back button then it should be either Activties or Fragments because both handle the back button natively. Fragments will need to be added to the FragmentManager back stack for the back button to work. Managing fragments, dialogs and the back stack can be a bit of an annoyance though!

UPDATE Sept 2018:

Some devs at Google are recommending single activity apps using the new navigation architecture component.

Answered by: John266 | Posted: 25-02-2022



Answer 3

Also keep in mind that implementing your app with multiple Activities will give the user a more coherent experience with the platform as a whole. Part of the experience will be shaped by using the built-in Google apps, so users will probably have an easier time using your application if it behaves similarly to the ones that are already installed on the phone.

Answered by: Grace350 | Posted: 25-02-2022



Answer 4

Different from others I use a mixture of both, for example,
1. There is a main menu when the application starts
2. You click on search, takes you to search activity
3. Then there's a filter button, which just switches view and shows you filter options
4. There are two buttons at the end of the filter view, You hit "Search" or "Cancel" and you are back to the Search View again (without switching activity)
5. Now if the user hits the phone back button he's taken back to the main menu instead of the search filter options. Which I guess is the correct behavior.

Use it the way user will feel natural. And keeping everything in one activity will make it complex.

Answered by: John782 | Posted: 25-02-2022



Answer 5

It all depends on application, what are you trying to achieve better performance, smoother UI. IMHO I prefer the second approach of controlling the Activities manually even that it is more complex as you have stated. This is a approach I have used in my android tabs project, also you might want to take a look at a class called ActivityGroup (not sure the package) it allows you to have multiple activities that you can switch between, good thing about this class is that your activities are not unloaded when you switch but a bad thing is it takes longer to load your main app.

Just my opinion.

Answered by: Brianna519 | Posted: 25-02-2022



Answer 6

The problem with switching views, that I stumbled upon, is also caused by garbage collector. Seems that GC is triggered when you leave activity and not the view. So, changing tabs with a fairly complex children views, for instance, will almost inevitably lead to stack overflow exception..

Answered by: Michael310 | Posted: 25-02-2022



Answer 7

I've experienced so many problems with multiple activity layout that I strongly discourage it, unless there's good reason to pick it.

Disadvantage of multiple activities

Using multiple activities it is much hard to refactor code to return data from activity.

If you call a 'sub'-activity then the main activity may be killed. But you never experience that while debugging on a decent device, hence you need to handle always saving state and correctly recovering state. That is a pain. Imagine calling a method on a library (ie. another activity), and you would have to be ensure that when that method returns your app must be able to recreate its state completely with all fields on all objects in the VM (ie. activity.restoreIntance). Its insane.

Also the other way round, when you open a subactivity the VM might have been killed since the subactivity was first spawned, such as when app is minimized while subactivity is displayed.

Its so much cleaner to just have one place to store the relevant app-state, and in my case, most often if VM is killed, I want to return user to main-screen, and let them do their stuff again, because I don't spend 30-50 hours coding save/resume functionality that 0.1% of users will ever experience.

Alternative

Fragments or just manage you activity views yourself. Managing views manually, requires coding some view-switching alternative to activities/fragments with transitions if desired.

And no it does not mean one mega-activity, as suggested in the accepted answer, in any other way than its one mega-app. It just requires a bit more design of the codebase into fitting pieces, because there's slightly more work managing views, though much less work managing activity-state and other weirdness.

Possibly relevant: Reddit: It's official : Google officially recommends single activity app architecture

Answered by: Sam768 | Posted: 25-02-2022



Similar questions

Does switching activities in Android start a fresh JVM

Does switching activities in Android start a fresh JVM? It seems like each activity is meant to run as its own "main" method. If I have a singleton (via Guice, not an actual singleton in this case) should I expect to be re-creating it every time I switch activities?


switching between activities in android

Hi I'm writing an app that has multiple activities. Right now it starts at the home screen, then when the user presses a button it starts a new activity and goes to another screen, then the user enters in information and presses a button to start another activity and another screen. I have a menu setup so that from whatever the activity the user is in they can get back to the home screen. What I want it to do is ki...


android - Why does my code leak when switching activities?

I have written a mapping application which can either use Google Maps or Open Street Map as a tile provider. The Google and OSM maps are displayed in separate activities. After the splash screen a select mode activity is entered. From this screen the user can either choose the Google or OSM activty via a button. I'd like to be able to switch between Google and OSM via a button in each mapping activity. When I code...


java - Runtime error while switching between activities in Android

I got a runtime error when I press the button that should change the activity: package com.example.LocationTracker; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class LocationTracker extends Activity{ /** Called when the activity is first created. */ Button btn_Tracker; ...


Android Issue switching activities

In my app, I have a normal menu where I can select from the menu and go to a different view. Simple... private void gotoSettings() { Intent settingsIntent = new Intent(this, SettingsActivity.class); startActivity(settingsIntent); } Now I also have a method in the main view that handles flings (swipes between views): Intent intent = new Intent(MainActivity.this.get...


android - Switching between different activities inside a Tab

I have a tab layout, inside which I am successfully able to lead an Activity. However, I would want such a mechanism so that I can have few buttons in the bottom, and when the user clicks them, I am able to load different Activities inside the same Tab. This functionality may closely resemble having Tabs inside a Tab, which, I feel, isn't a good practice and will make the things quite unpredictable.


how to change image in android while switching between activities

in my app in activity A i have a list view showing some details along with an image at the bottom and a button at the top. When the user clicks in the list it opens a new activity B. If the user clicks the back button he returns back to activity A and there i want to show a new Image in the image View. At the same time there should not be any change in the activity A. In iPhone we are to use a method calle...


Android: Switching between two activities and sharing information between them

This is a basic question but I need some help with it. I have two activities : actA, actB. While in actA I want to start actB and give it a String, than I want to end actB and return another String to actA (I don't want to go to onCreate() of actA, I would much rather return this value to some method in actA so it can use the String from actB. Help is appreciated


android - Switching between activities within a tab bar layout

I have this basic tab bar layout that creates a tab for three activities. What if I wanted to swap the activities within of the main activities? I want it to work like the Contacts app where you have your main contacts tab and a detailed activity when you click on a contact. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(...


Android: Switching between two activities

I have this code : Button groupsButton = (Button)findViewById(R.id.groupsButton); groupsButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent myintentGroups=new Intent(CreateMessageActivity.this, GroupsActivity.class).putExtra("<StringName>", "Value"); startActivityForResult(myintentGroups, 3); } });


java - Playing BG Music Across Activities in Android

Hello! First time to ask a question here at stackoverflow. Exciting! Haha. We're developing an Android game and we play some background music for our intro (we have an Intro Activity) but we want it to continue playing to the next Activity, and perhaps be able to stop or play the music again from anywhere within the application. What we're doing at the moment is play the bgm using MediaPlayer at ou...


How to stay connected to an Android service between multiple activities?

I have multiple activities and one service. In MainActivity I successfully connect to service (using a class that implements ServiceConnection + bindService() + startService()) but when I try to apply same method in other activity I see in LogCat a error: 01-15 22:29:37.438: ERROR/ActivityThread(12206): android.app.ServiceConnectionLeaked: Activity...


android - Is it better to create new Activities or just create a different Layout and replace the existing layout?

Since I am new to Android, I am now thinking on what is the correct way of doing things. As it stands, the application I'm writing has 4 different screens: Screen 1 - list of nodes (main screen) Screen 2 - options menu, tableLayout with buttons Screen 3 - navigation Screen 4 - text details on version etc These screens can be navigated to/from using a "header" ...


android - freeze on sending certain bitmaps to activities

Basically I receive the Image's URI from the Gallery, then created a Bitmap and want to send to another activity for displaying: Uri imageUri = intent.getData(); mBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri); Intent intent = new Intent(TakePictureActivity.this, PreviewActivity.class); intent.putExtra(EXTRA_BITMAP_DATA, mBitmap); startActivityForResult(intent, REQUEST_PREVIEW); ...


problem in passing data in android Activities?

can any one guide me what mistake am i doing in this code??? it not seems to be working.. i have two activies public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent intent = new Intent(DataPassing.this, DataPassing2.class); Bundle b = new Bundle(); b.putInt("key", 1123); intent.putExtras(b); ...


Android : launching diff activities under TabWidget

I am trying to launch activities under each tab. I have tried with following code public class Tab_Proj1 extends TabActivity { TabHost mTabHost ; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Context context = getApplicationContext(); //mTabHost = (TabHost) this.findViewById(R.id.); mTabHost = getTabHost(); mTabHost.setup();...


Android app with multiple activities

I have a very simple game that consists of only one activity, and I want to add a title screen. If the title screen is another activity, what changes do I need to make to my manifest file to make the title screen open first? The gameplay activity is called Leeder, and the title screen activity is called LeederTitleScreen here is my current manifest file. <?xml version="1.0" encodi...


xml - Android - Should I use multiple activities or multiple content views

i'm new with android. I'm working on an application using xml layouts. I wish to know which is better: 1. Use few activities and change its contentview 2. Use an activity for each 'view' needed If both works, in which case which option would be better? thx a lot


android - Sending data between activities within my app?

I have a TabActivity, and the tabs point to sub activities. Is there a way I can send a 'message' to those child activities? I just want to pass a string across, not sure if this is possible. I have some data being fetched by the parent TabActivity, and the child tabs can't do anything useful until the parent is done fetching. When fetching is complete, I'd like to pass that data to the child activities so they ca...


java - When to use new layouts and when to use new activities?

I'm making a game in Android and I'm trying to add a set of menu screens. Each screen takes up the whole display and has various transitions available to other screens. As a rough summary, the menu screens are: Start screen Difficult select screen Game screen. Pause screen. Game over screen. And there are several different ways you can transition between scre...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top