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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:13:00+00:00 2026-06-01T14:13:00+00:00

I have a simple game activity. I start it and than press the back

  • 0

I have a simple game activity. I start it and than press the back button. When it is pressed it calls finish(). After I start the game again. I go through the onCreate() and recreate the global variables I have but then I receive a broadcast from another class and when I debug it I am in another activity called activity$1 and the one before was activity $0. Can I force the first one to be killed when I call finish()? Or how can I get the already running instance with the set variables from the second one?

My code is below with some comments

public class actvty extends Activity {

private LocalBroadcastManager brdcst_manager;
private BroadcastReceiver receiver;
private RelativeLayout lay;
private BallView ball_view;
private RingView ring_view;
private Background bg;
private CounterVIew counter_view;
private long startTime;
private int count_games = 0;
private List<Integer> turns = new ArrayList<Integer>(), times = new ArrayList<Integer>();

public final static String FINISHED = "com.blq.blq.blq.finished", EXIT = "com.blq.blq.blq.exit";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    startTime = System.currentTimeMillis();

    //This manager is used to handle broadcasts within the application
    brdcst_manager = LocalBroadcastManager.getInstance(this.getApplicationContext());

    //Set the main layout
    setContentView(R.layout.main);

    //Get the layout from the resources
    lay = (RelativeLayout) findViewById(R.id.Layout);

    //Set the game to full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    //Create the background for the game 
    bg = new Background(this.getApplicationContext());

    //Create the counter view
    counter_view = new CounterVIew(this.getApplicationContext());

    //Create the rings and add them to the stage
    ring_view = new RingView(this.getApplicationContext());

    //Create the balls
    ball_view =  new BallView(this.getApplicationContext(), ring_view);

    //Display the first picture
    counter_view.display_next();

    //Add the views to the stage
    lay.addView(bg);
    lay.addView(counter_view);
    lay.addView(ring_view);
    lay.addView(ball_view);

    //Add the events that we are going to listen for to the filter
    IntentFilter filter = new IntentFilter();
    filter.addAction(FINISHED);
    filter.addAction(EXIT);

    //Create the receiver
    receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(FINISHED)) {
                //HERE IS THE PROBLEM... WHEN I RESTART THE GAME I GO THROUGH THE onCreate AND I CREATE THE GLOBAL VARIABLES
                //BUT HERE THEY ARE ALL NULL WHEN I DEBUG IT
                Log.v("asdasd", FINISHED);
                reset_game();
            } else if(intent.getAction().equals(EXIT)) {
                finish();
            }
        }
    };

    //Register the listener
    brdcst_manager.registerReceiver(receiver, filter);
}

protected void reset_game() {
//HERE IS THE PROBLEM... WHEN I RESTART THE GAME I GO THROUGH THE onCreate AND I CREATE THE GLOBAL VARIABLES
//BUT HERE THEY ARE ALL NULL
//ALSO WHEN THIS CHRASHES BECAUSE OF THE NULL VARIABLES I GET A MESSAGE IN THE LOG CAT THAT SAYS THAT
//THE INTENT WAS TRIGGERED BY "ACTIVITY$0" AND WAS RECEIVED BY "ACTIVITY$1" WHICH MAKES ME THINK THAT
//I'VE SOMEHOW GOT ANOTHER ONE WHILE THE FIRST ONE IS STILL OPEN
    turns.add(new Integer(ball_view.getCount_turns()));
    times.add(new Integer((int)(System.currentTimeMillis() - startTime)/1000));
    count_games++;
    ball_view.display_newxt();
    counter_view.display_next();
    try {
        lay.invalidate();
        ball_view.invalidate();
        counter_view.invalidate();
        lay.invalidate();
        Thread.sleep(5000);
    } catch (Exception e) {
        e.printStackTrace();
    }
    counter_view.display_next();

    startTime = System.currentTimeMillis();

    //PRINT FOR TESTING
    Log.v("TURNS!!!!!!", turns.toString());
    Log.v("CHRONOMETER!!!", times.toString());
    Log.v("NUMBER OF GAMES!!!!", String.valueOf(count_games));
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        finish();
    }
    return super.onKeyDown(keyCode, event);
  }

@Override
protected void onDestroy() {
//IF I DO NOT NULL THE VARIABLES HERE THAN WHEN I RESTART THE APPLICATION 5 TO 10 TIMES DEPENDING ON THE RESOLUTION
//I GET AN ERROR OOM AND MY BITMAP IS TOO BIG... :(
    super.onDestroy();

    lay.removeAllViews();
    bg.destroy();
    counter_view.destroy();
    ring_view.destroy();
    ball_view.destroy();
    bg = null;
    counter_view = null;
    ring_view = null;
    ball_view = null;
    System.gc();

Thanks is advance.

  • 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-01T14:13:01+00:00Added an answer on June 1, 2026 at 2:13 pm

    I found the problem. Even though the old activity is still in the memory if I unregister the receiver in the onDestroy it will at least not receive the call and all will be well. 🙂 I guess if I null a variable I should null em all not just the once that I need 🙂

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

Sidebar

Related Questions

I'm making a simple Android game written in Java. I have my activity... public
I have a very simple game that consists of only one activity, and I
I have a simple JavaScript game that sends a score to PHP by adding
Say I have simple program that emulates a board game with a number of
I'm writing a simple game and I'm going to have the mouse control the
I have a simple tank wars style game using the allegro open source library.
Trying to make simple minesweeper game in python, but have one problem. I have
I'm working on a simple board game in qt, and i have some problem
If I'm making a simple grid based game, for example, I might have a
I have a simple game that is in progress. As of right now all

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.