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 7515217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:44:07+00:00 2026-05-30T00:44:07+00:00

My Android app has a tab bar that uses an Intent to launch the

  • 0

My Android app has a tab bar that uses an Intent to launch the content in each tab, as in Android’s API Demos example. I’ve put an AndEngine activity under one of the tabs. My first question is: is this a reasonable idea? Or if I’m using AndEngine, is it better to implement my app’s whole UI in AndEngine?

Assuming I’m not already onto a non-starter, the specific problem I’ve run up against is as follows. This AndEngine activity – the one I’ve incorporated in my tab-based app – is a simplified version of the AndEngine PinchZoom example. It works well until you switch away from the tab and then back again. At that point, the view reappears, but you can’t scroll or zoom any more.

Here’s my activity class. Let me know if you want to see the tab bar class too. As you can see, I’ve tried to switch off the various listeners and detectors when the user leaves the tab, and switch them back on when they return. But I’ve established, using a breakpoint, that onSceneTouchEvent isn’t called when I touch the screen after returning to the tab. Is something intervening and capturing my touch events? Or is there something that goes inactive and needs to be brought back to life?

I’ve also posted this question on the AndEngine forums.

Edit:

Thanks for your suggestions, Guillaume. You’re right that onResumeGame isn’t called when the user returns to the tab. I thought I’d checked that, but I must have confused myself by putting the scrollable graphic under two tabs at once. The onResume method is called at that point, while onCreateGame isn’t. I’ve therefore changed the code so that the touch detectors are switched on in onResume. This method is definitely being called when I want it to be, so I guess that’s progress, but onSceneTouchEvent still isn’t being triggered when I touch the screen after returning to the tab. The updated code is below, with the changes marked as // NEW.

public class HomeActivity extends SimpleBaseGameActivity implements IOnSceneTouchListener,
    IScrollDetectorListener, IPinchZoomDetectorListener {
    private static final int CAMERA_WIDTH = 480;
    private static final int CAMERA_HEIGHT = 628;

    private boolean mInitialised = false; // NEW
    private Scene mScene;
    private ZoomCamera mZoomCamera;
    private ITexture mTexture;
    private ITextureRegion mGrassTextureRegion;
    private SurfaceScrollDetector mScrollDetector;
    private PinchZoomDetector mPinchZoomDetector;
    private float mPinchZoomStartedCameraZoomFactor;

    @Override
    public EngineOptions onCreateEngineOptions() {
        this.mZoomCamera = new ZoomCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        final EngineOptions engineOptions = new EngineOptions(true,
            ScreenOrientation.PORTRAIT_FIXED,
            new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mZoomCamera);

        if (MultiTouch.isSupported(this)) {
            if (MultiTouch.isSupportedDistinct(this)) {
                Toast.makeText(this, "MultiTouch detected.", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(
                    this,
                    "MultiTouch detected, but your device has problems distinguishing between "
                        + "fingers.", Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(this, "Sorry your device does NOT support MultiTouch!",
                Toast.LENGTH_LONG).show();
        }

        return engineOptions;
    }

    @Override
    public void onCreateResources() {
        try {
            this.mTexture = new BitmapTexture() {
                @Override
                protected InputStream onGetInputStream() throws IOException {
                    return getAssets().open("gfx/background_grass.png");
                }
            }.load(this.getTextureManager());
            this.mGrassTextureRegion = TextureRegionFactory.extractFromTexture(mTexture);
        } catch (IOException e) {
            Debug.e(e);
        }
    }

    @Override
    public Scene onCreateScene() {
        this.mEngine.registerUpdateHandler(new FPSLogger());
        this.mScene = new Scene();
        this.mScene.setOnAreaTouchTraversalFrontToBack();
        this.mScene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

        // Calculate the coordinates for the sprite, so it's centered on the
        // camera.
        final float centerX = (CAMERA_WIDTH - this.mGrassTextureRegion.getWidth()) / 2;
        final float centerY = (CAMERA_HEIGHT - this.mGrassTextureRegion.getHeight()) / 2;

        // Create the sprite and add it to the scene.
        final Sprite grass = new Sprite(centerX, centerY, this.mGrassTextureRegion,
            this.getVertexBufferObjectManager());
        this.mScene.attachChild(grass);

        enableTouchDetectors(); // NEW
        mInitialised = true; // NEW

        return this.mScene;
    }

    @Override
    public void onPause() {
        this.mScene.setTouchAreaBindingOnActionDownEnabled(false);
        this.mScene.setOnSceneTouchListener(null);
        this.mPinchZoomDetector = null;
        this.mScrollDetector = null;
        super.onPause();
    }

    // NEW method
    @Override
    public void onResume() {
        super.onResume();
        // If returning to the tab, switch the touch detectors back on.
        if (mInitialised) {
            enableTouchDetectors();
        }
    }

    // This method has been removed since Guillaume's answer.
//    @Override
//    public void onResumeGame() {
//        super.onResumeGame();
//        this.mScrollDetector = new SurfaceScrollDetector(this);
//        this.mPinchZoomDetector = new PinchZoomDetector(this);
//        this.mScene.setOnSceneTouchListener(this);
//        this.mScene.setTouchAreaBindingOnActionDownEnabled(true);
//    }

    @Override
    public void onScrollStarted(final ScrollDetector pScollDetector, final int pPointerID,
        final float pDistanceX, final float pDistanceY) {
    }

    @Override
    public void onScroll(final ScrollDetector pScollDetector, final int pPointerID,
        final float pDistanceX, final float pDistanceY) {
        final float zoomFactor = this.mZoomCamera.getZoomFactor();
        this.mZoomCamera.offsetCenter(-pDistanceX / zoomFactor, -pDistanceY / zoomFactor);
    }

    @Override
    public void onScrollFinished(final ScrollDetector pScollDetector, final int pPointerID,
        final float pDistanceX, final float pDistanceY) {
    }

    @Override
    public void onPinchZoomStarted(final PinchZoomDetector pPinchZoomDetector,
        final TouchEvent pTouchEvent) {
        this.mPinchZoomStartedCameraZoomFactor = this.mZoomCamera.getZoomFactor();
    }

    @Override
    public void onPinchZoom(final PinchZoomDetector pPinchZoomDetector,
        final TouchEvent pTouchEvent, final float pZoomFactor) {
        this.mZoomCamera.setZoomFactor(this.mPinchZoomStartedCameraZoomFactor * pZoomFactor);
    }

    @Override
    public void onPinchZoomFinished(final PinchZoomDetector pPinchZoomDetector,
        final TouchEvent pTouchEvent, final float pZoomFactor) {
    }

    @Override
    public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
        this.mPinchZoomDetector.onTouchEvent(pSceneTouchEvent);

        if (this.mPinchZoomDetector.isZooming()) {
            this.mScrollDetector.setEnabled(false);
        } else {
            if (pSceneTouchEvent.isActionDown()) {
                this.mScrollDetector.setEnabled(true);
            }
            this.mScrollDetector.onTouchEvent(pSceneTouchEvent);
        }

        return true;
    }

    // NEW method
    private void enableTouchDetectors() {
        this.mScrollDetector = new SurfaceScrollDetector(this);
        this.mPinchZoomDetector = new PinchZoomDetector(this);
        this.mScene.setOnSceneTouchListener(this);
        this.mScene.setTouchAreaBindingOnActionDownEnabled(true);
    }
}
  • 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-30T00:44:08+00:00Added an answer on May 30, 2026 at 12:44 am

    Tommy:
    I had this same scenario in my own app, in which I needed three tabs with one being andEngine. But i found that TabActivity did not re-initialize the tabs in the same way that firing a new Intent does. If you log the lifecycle of one of your other tabs, you will see this is not just an andengine thing, but a TabActivity thing.
    There is a really useful thread here about the issue:
    http://groups.google.com/group/android-developers/browse_thread/thread/d89f1d5f913bb6f7

    Including an example that sets each tab as views within a single activity. here is the link. I’ve used code from commonsware myeslf and found their libraries and examples to be well tested and high-quality.
    https://github.com/commonsguy/cw-android/tree/master/Fancy/Tab/

    In anycase, I could not find a way to make a tabactivity work without re-writing the core classes of Andengine, which was beyond the scope of what I could do in the project.

    So what I did as a workaround was this:
    I build the tabs myself as three activities that shared a footer that lets you swap between them. I had to monkey around a bit with the history, and override the behavior of the back button to keep within the tabs when appropriate.
    Also, I used an intent flag in the Android manifest:

    android:launchMode="singleInstance"
    

    Which helped the app run smoothly by assuring that there was only ever one instance of the Andengine tab ever created.

    Hope this helps. It would be great if andengine could be made compatible with TabActivity, but I do not think it is fully at this moment.

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

Sidebar

Related Questions

In my android app, Tab Bar has 3 tabs in it in Tabs activity
I'm in the process of making an Android 3 app that has a tab-based
I've got some XML resources in my Android app. Each one has a different
My Android app has a WebView that requires the Flash plugin. The webview will
I'm looking for a way to detect that my android app has been backgrounded,
My Android app, which uses a TabActivity to display its contents, has 4 tabs
My Android app has a layout that looks like this: --------------------- | | |
I have an android app that has to be restricted to portrait mode, and
I am developing an Android app with fragments that has many possible flows. With
I have an Android app that uses the NotificationManager to inform the user of

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.