Android 不需要的第二个活动已启动
我有一个简单的游戏活动。我启动它,然后按下后退按钮。当按下它时,它调用finish。在我重新开始比赛之后。我执行onCreate并重新创建我拥有的全局变量,但随后我从另一个类接收到一个广播,当我调试它时,我处于另一个名为activity$1的活动中,之前的活动是activity$0。当我叫完后,我能强迫第一个被杀吗?或者,如何从第二个实例中获取已运行的实例以及设置的变量 下面是我的代码和一些注释Android 不需要的第二个活动已启动,android,android-activity,kill,Android,Android Activity,Kill,我有一个简单的游戏活动。我启动它,然后按下后退按钮。当按下它时,它调用finish。在我重新开始比赛之后。我执行onCreate并重新创建我拥有的全局变量,但随后我从另一个类接收到一个广播,当我调试它时,我处于另一个名为activity$1的活动中,之前的活动是activity$0。当我叫完后,我能强迫第一个被杀吗?或者,如何从第二个实例中获取已运行的实例以及设置的变量 下面是我的代码和一些注释 public class actvty extends Activity { private Lo
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();
谢谢,这是预付款。我发现了问题。即使旧活动仍在内存中,但如果我在onDestroy中注销接收器,它至少不会收到呼叫,一切都会好起来我想如果我将一个变量设为null,我应该将它们全部设为null,而不仅仅是我需要的一次: