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

  • SEARCH
  • Home
  • 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 6707019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:35:21+00:00 2026-05-26T07:35:21+00:00

I have an app with two screens, and buttons that switch between them. One

  • 0

I have an app with two screens, and buttons that switch between them. One screen is mainly a listView, but the other has a game engine in it (AndEngine) and it can take a while for the game engine to init the first time the activity is created.
So switching between them can cause big delays when going to a new game Engine. However, if I just use the back button, the previous game engine loads much faster.
Is there a way I can specify to crete only one game engine and always bring up that instance of the activity? Right now I am using startIntent() to swap screens. Is there some other way?
A way that allows me to just have a single instance of each activity and swap between them?

  • 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-05-26T07:35:22+00:00Added an answer on May 26, 2026 at 7:35 am

    Android provides a simple way to switch between activities using a TabHost. You can also use it to switch between Fragments as explained under TabActivity. Alternatively, you can add a FrameLayout to your activity, programmatically instantiate the fragments and attach/show/hide them when needed.

    Your res/layout/main.xml would look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_height="fill_parent"
                 android:layout_width="match_parent"
                 android:id="@+id/mainframe">
    </FrameLayout>
    

    And assuming the v4 support library is used, your activity would look like this:

    public MyActivity extends FragmentActivity {
    
        private Fragment mListFrag;
        private Fragment mGameFrag;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            mListFrag = new MyListFragment();
            mGameFrag = new MyGameFragment();
    
            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction()
                .add(
                    R.id.mainframe,
                    mListFrag,
                    MyListFragment.class.getName())
                .add(
                    R.id.mainframe,
                    mGameFrag,
                    MyGameFragment.class.getName())
                .detach(mGameFrag)
                .commit();
    
            fm.executePendingTransactions();
        }
    
        public void showList() {
            getSupportFragmentManager().beginTransaction()
                .hide(mGameFrag)
                .show(mListFrag)
                .commit();
        }
    
        public void showGame() {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            if (mGameFrag.isDetached()) {
                ft.attach(mGameFrag);
            }
            ft.hide(mListFrag).show(mGameFrag).commit();
        }
    }
    

    Observe that MyGameFragment.onCreateView isn’t called until it is first attached. After that, hiding and showing the fragments allows the user to switch without delay.

    Edit: I realise now you wanted 2 activities with their own button. I’ve updated the code to reflect this. From the OnClickListeners simply call the relevant activity functions like so:

    ((MyActivity) MyListFragment.this.getActivity()).showGame();

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

Sidebar

Related Questions

I have two screens on my app that I want to present to users,
I have two buttons in my flex app next to each other, ButtonA and
i have an app with two buttons. On startup the first screen is a
Is it possible to have two app.config files where one app.config serves as a
I have an app that has a centre view with two views off to
I have a universal iOS app with a custom view that has three buttons,
I have a main view screen with navcontroller in my app with two UIbuttons.
So I have two view controller that I load on the app delegate when
My app has 5 different relative layouts that it can switch between by using
I was working on an app tonight and I noticed that I have two

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.