Reputation: 11
I have two activity.First activity is game screen with pause button and second activity is with resume and restart button.When i clicked pause button over Game screen then i goes to second activity from where when i clicked restart button then i want to finish first activity(Game screen) and want to start new game so this is my problem how to finish first activity please help me
Upvotes: 0
Views: 1498
Reputation: 1025
A suggestion would be that you register a broadcast receiver in all your activities with "finish()" in their onReceive() & whenever you wish to quit you can simple pass an intent indicating that all activities must shut down..... Although make sure that you do "unregister" the receiver in your onDestroy() methods.
something like this
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addAction("end");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
finish();
}
};
registerReceiver(receiver, filter);
}
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(receiver);
}
And when you wish to finish this from another activity just do this
Intent i = new Intent("end");
sendBroadcast(i);
Upvotes: 3
Reputation: 54672
Option 1: use Fragments
Option 2:
from first activity use startActivityForResult
then when the second activity is finished cathc in onActivityResult
. There finish the first activity then start game.
Option 3: Send a broadcast message and in the first activity receive it and finish it.
There might be other options. Just put down some options which came immediately in my mind. If I were you I would use Option 1
Upvotes: 1
Reputation: 11085
This will kill all top activities and start new Activity NewGame.
Intent intent = new Intent(getApplicationContext(), NewGame.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Upvotes: 1