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?
I have an app with two screens, and buttons that switch between them. One
Share
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:
And assuming the v4 support library is used, your activity would look like this:
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();