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" View that is placed on top. The header then has 4 different buttons:

+--------------------+
| menu with buttons  |
+--------------------+
|                    |
|                    |
|                    |
|  C O N T E N T     |
|                    |
|                    |
|                    |
+--------------------+

main.xml is really just a LinearLayout that INCLUDES the header.xml and then the content, in that case the list of nodes in a ListView

options.xml is the same thing almost, it includes the headerxml and then a bunch of buttons...

...and so on with the two other screens.

So, when I press one of the buttons in the header/menu on top the content should be switched to that screen. My question is:

  • Should I create one Activity for each screen? I read on Google that:
    An activity presents a visual user interface for one focused endeavor the user can undertake. So that can be interpreted that I should use one Activity for each of these screens.

  • Should I not create more Activities than the startup, and then just run the setContentView(R.layout.whatever) when I want to change the "content" above?


Asked by: Adelaide236 | Posted: 25-01-2022






Answer 1

You should probably use a separate Activity for each screen; otherwise you need to end up keeping track of which individual View is currently being displayed, plus the state of all those not currently being displayed when the user switches to another window, or a call comes in etc.

It's easier to keep track of this state if you just use a separate Activity for each piece of functionality.

If you do decide to keep everything in a single Activity however, you could look at the TabActivity class. However, there are also caveats there that prevent you from having an Activity as the tab content.


Regarding your follow-up, you unfortunately cannot attach an Intent directly to a Button like you can with a MenuItem via the XML, however you could just extend Activity to make your own common base class with some code that hooks up the listeners.

Something like:

public class BaseActivity extends Activity {
    protected View.OnClickListener mButtonListener;

    protected void setupHeaderButtons() {
        findViewById(R.id.header_btn_1).setOnClickListener(mButtonListener);
        // ...
        findViewById(R.id.header_btn_n).setOnClickListener(mButtonListener);
    }
}

public class FirstActivity extends BaseActivity {
    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.first_activity);

        // This needs to be done *after* the View has been inflated
        setupHeaderButtons();
    }
}

Answered by: Chloe229 | Posted: 26-02-2022



Answer 2

I am also quite new to Android but my advice would be to create 4 different Activities. The reason for that is that it seems like a "cleaner" implementation to me. Sure, there is more code to be written but I'd rather have more small classes than one big class with lots of code in it.

Answered by: Brad732 | Posted: 26-02-2022



Answer 3

Not sure if this has been mentioned in any of the sub questions, but if you change activities pre-2.0 you cannot animate between them.

So if you have a loading screen and would like it to fade to a menu you have to use two views and switch between the two.

Answered by: Catherine436 | Posted: 26-02-2022



Similar questions

android - How do I switch between existing activities?

I am designing an app which is a kind of articles reader & manager and articles available in different languages. So when user reads a particular article in Russian there is a button to display the same article in English and vice versa. To do that I start new activity. What I have now is that if user presses "translate" button of the same article several times there will be heaps of duplicate activitie...


Switch between existing activities in Android

So I am developing this small game, it currently uses 2 activities: The Menu Activity and the actual Game Activity; The Game Activity is started by the Menu Activity (no problems at that) Then I can switch from the Game Activity to the Menu Activity by using the back Key: public void onBackPressed() { super.onBackPressed(); } Now I am back in the Menu Activity. The Game Activit...


java - How do I use methods from an existing running thread in different Activities

I got the following problem. My App has 5 activities and works as TCP-Client. If I start the App, activity 1 gets started and establishes a connection with a server. This activity owns a listener for getting incomming Data. public void messageReceived(final String message) { RemoteActivity.this.runOnUiThread(new Runnable() { @Override public void run() { Remot...


android - How to check for existing activities when back button pressed

Generally, when the back button is clicked, I call finish(), and I am taken back to the previous activity (which is my MenuActivity) : @Override public void onBackPressed() { finish(); } However, sometimes the there are no other activities running when the back button is clicked and the app simply exits. Is there a way I can check if an ac...


android - Remove all activities of an app from every existing task in Activity Back Stack

Is there a way of removing all activities belonging to the app in foreground (my app)? The activities might be present in different tasks. Also after removing all activities my app should return to home screen which is launcher activity of my app. Any kind of help would be greatly appreciated.


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: 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?


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