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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:12:03+00:00 2026-06-08T09:12:03+00:00

I have a simple application, where I created a SurfaceView class, then in the

  • 0

I have a simple application, where I created a SurfaceView class, then in the main activity, I created an object of the class, then added this SurfaceView to a relative layout, and set the content view to the relative layout.

package com.my.game;
import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;

public class ShootLetters extends Activity {

    private Panel panel;
    private int newWidth;
    private int newHeight;

    private AdView adView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bitmap bg = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.shooting_background);
        ;

        int width = bg.getWidth();
        int height = bg.getHeight();

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        newWidth = metrics.widthPixels;

        newHeight = metrics.heightPixels;
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // CREATE A MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scaleWidth, scaleHeight);

        Bitmap resizedBackGround = Bitmap.createBitmap(bg, 0, 0, width, height,
                matrix, false);

        Window win = getWindow();
        Drawable d = new BitmapDrawable(resizedBackGround);
        win.setBackgroundDrawable(d);

        panel = new Panel(this, newWidth, newHeight);

        panel.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    panel.setClickX(event.getX());
                    panel.setClickY(event.getY());
                }
                return true;
            }

        });

        adView = new AdView(this, AdSize.BANNER, "admobidxxx222");

        RelativeLayout rl = new RelativeLayout(this);
        rl.addView(panel);
        rl.addView(adView);

        setContentView(rl);

        // Initiate a generic request to load it with an ad
        adView.loadAd(new AdRequest());
    }

    @Override
    public void onDestroy() {
        if (adView != null) {
            adView.destroy();
        }
        panel.stopThread();
        panel = null;

        super.onDestroy();
    }

}

This works fine for starting and stopping the application.

The problem is when another activity comes on top of this one, it behaves in an unstable way (no need for details at this stage as sometimes it crashes and sometimes it works with missing layout objects).

I read that I should implement onPause() & onResume() methods to handle this.
I only need pause the activity or resume it on resume. No persistent data required.

What should I put in which methods in this case?

Thanks

  • 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-08T09:12:04+00:00Added an answer on June 8, 2026 at 9:12 am

    I guess its your Panel that is the surfaceview? I see you are doing something with its thread in onDestroy but that is not sufficient. You had a correct hunch about implementing onPause() and onResume() Now my idea(without being able to see all of your code) is that you are not dealing with the thread correctly. In your Panel class which extends surfaceview you should have something like these two functions:

        public void pause(){
            Loop = false;
            while (true){
                try{
                    panelThread.join();//end it
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                break;
            }
            panelThread = null;
        }
        public void resume(){
            Loop = true;
            panelThread = new Thread(this);
            panelThread.start();
        }
    

    Ignore the Loop part if you are not using a loop in your run() method.

    Then, in your activity’s onPause() and onResume() methods you call panel.pause() and panel.resume() respectively. This makes sure that the surface view thread is being handled in a safe way.

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

Sidebar

Related Questions

I have created blank C#/XAML Windows 8 application. Add simple XAML code: <Page x:Class=Blank.MainPage
I have created a simple application in android [PhoneGap] [Eclipse] {Sorry for this convention
I am new to visual studio.I have created a simple console application and then
Hi I have created a simple Main application and two InjectedApplications (InjectedApplication1 and InjectedApplication2)
I have created a simple blog application with Ruby on Rails. The applications consists
I have created a simple win 32 application..in which it has a textbox and
We have created a simple wix project for a basic windows application. Everything builds
I created a simple ASP.NET MVC version 1.0 application. I have a ProductController which
I have created a simple Facebook application in PHP, that greets user by there
I have created simple application in windows sharepoint services. Now I want to deploy

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.