Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8838169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:55:18+00:00 2026-06-14T09:55:18+00:00

I am trying to create a tablet application with fragments. The left part of

  • 0

I am trying to create a tablet application with fragments. The left part of the screen will have four buttons and the right part of the screen will change depending on what button was clicked.
I have created main activity and four fragments. Each fragment has its own layout with several TextView fields. When applications starts it loads all fragments to RAM – this way it can keep the fragments status so that when user switch from one fragment to another all the text fields keep their text values until he clicks the final submit button. The app is based on SDK 4.1. The app is a little bit slow especially when it starts. I was wondering if it has been designed properly and if there are some way to improve it?

Below is the main activity class:

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    private Button buttonOne;
    private Button buttonTwo;
    private Button buttonThree;
    private Button buttonFour;

    private Fragment fragmentOne;
    private Fragment fragmentTwo;
    private Fragment fragmentThree;
    private Fragment fragmentFour;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonOne = (Button) findViewById(R.id.button_one);
        buttonTwo = (Button) findViewById(R.id.button_two);
        buttonThree = (Button) findViewById(R.id.button_three);
        buttonFour = (Button) findViewById(R.id.button_four);

        fragmentOne = new FragmentOne();
        fragmentTwo = new FragmentTwo();
        fragmentThree = new FragmentThree();
        fragmentFour = new FragmentFour();

        FragmentTransaction fragmentTransaction = getFragmentManager()
                .beginTransaction();

        fragmentTransaction.add(R.id.frameLayout_one, fragmentOne);
        fragmentTransaction.add(R.id.frameLayout_one, fragmentTwo);
        fragmentTransaction.add(R.id.frameLayout_one, fragmentThree);
        fragmentTransaction.add(R.id.frameLayout_one, fragmentFour);

        fragmentTransaction.commit();

        buttonOne.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                FragmentTransaction fragmentTransaction = getFragmentManager()
                        .beginTransaction();
                fragmentTransaction.show(fragmentOne);
                fragmentTransaction.hide(fragmentTwo);
                fragmentTransaction.hide(fragmentThree);
                fragmentTransaction.hide(fragmentFour);

                fragmentTransaction.commit();

            }
        });

        buttonTwo.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                FragmentTransaction fragmentTransaction = getFragmentManager()
                        .beginTransaction();
                fragmentTransaction.hide(fragmentOne);
                fragmentTransaction.show(fragmentTwo);
                fragmentTransaction.hide(fragmentThree);
                fragmentTransaction.hide(fragmentFour);

                fragmentTransaction.commit();

            }
        });
        buttonThree.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                FragmentTransaction fragmentTransaction = getFragmentManager()
                        .beginTransaction();
                fragmentTransaction.hide(fragmentOne);
                fragmentTransaction.hide(fragmentTwo);
                fragmentTransaction.show(fragmentThree);
                fragmentTransaction.hide(fragmentFour);

                fragmentTransaction.commit();

            }
        });
        buttonFour.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                FragmentTransaction fragmentTransaction = getFragmentManager()
                        .beginTransaction();
                fragmentTransaction.hide(fragmentOne);
                fragmentTransaction.hide(fragmentTwo);
                fragmentTransaction.hide(fragmentThree);
                fragmentTransaction.show(fragmentFour);

                fragmentTransaction.commit();

            }
        });

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:55:20+00:00Added an answer on June 14, 2026 at 9:55 am

    Consider using a FragmentStateViewPager.

    In an ideal world, if you have several Fragments you are switching between like in your case, you’ll probably not want to keep them around in RAM. 2 or 3 fragments is probably okay, 4 or more is starting to push it.

    The FragmentStateViewPager will automatically detach and save the state of your Fragments as they move “off screen”

    You will have to implement onSaveInstanceState in each one of your fragments to save any state of any non-UI member variables (UI Views save their state automagically). Then restore them in onCreate or onCreateView.

    *Of course, if your Fragments’ onCreate/onCreateViews are slow, then this won’t help much. First make sure your Fragments’ creations are fast… Start by looking for code that can be moved from onCreate/onCreate view to onResume.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create an application which will have different views for mobile,
I am trying to create an application for tablets that has a main screen
I am writing a google reader application and have been trying to create the
I'm trying to create the following layout for a mobile/tablet application using GWT +
i have an application that am trying to implement it on a tablet 7'.
I am trying to create an application which requires a two table SQLite database
I currently have my application set up with a ListFragment on the left and
I'm creating a Ruby on Rails application, and I'm trying to create/login/logout users. This
I am trying to create a LWUIT Table in my J2ME application where all
I am trying create a small web application that allows a user to login

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.