【问题标题】:Unwanted second activity launched启动了不需要的第二个活动
【发布时间】:2012-04-03 19:24:42
【问题描述】:

我有一个简单的游戏活动。我启动它,然后按后退按钮。当它被按下时,它会调用finish()。在我重新开始游戏之后。我通过 onCreate() 并重新创建了我拥有的全局变量,但随后我收到了来自另一个类的广播,当我调试它时,我处于另一个名为 activity$1 的活动中,而之前的一个活动是活动 $0。当我调用finish()时,我可以强制杀死第一个吗?或者如何从第二个实例中获取已设置变量的已运行实例?

我的代码在下面有一些 cmets

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();

先谢谢了。

【问题讨论】:

    标签: android android-activity kill


    【解决方案1】:

    我发现了问题。即使旧的活动仍在内存中,如果我在 onDestroy 中取消注册接收器,它至少不会收到呼叫,一切都会好起来的。 :) 我想如果我将一个变量设为空,我应该将它们全部设为空,而不仅仅是我需要的一次:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-27
      • 1970-01-01
      相关资源
      最近更新 更多