Let's pretend I have the activities MyActivityA
and MyActivityB
.
MyActivityA
startsMyActivityB
MyActivityB
starts an repeating AlarmManager who triggers anIntentService
which is informingMyActivityB
with aResultReceiver
whensomething
happens.something
happened,MyActivityB
opens anAlertDialog
, the PositiveButton leads toMyActivityA
(IntentService
makes GET-Requests, something
is an expected GET-Response)
The first time it's working. But when I then repeat step one and two, the AlertDialog
does not appear and the app crashes with BadTokenException: Unable to add window -- token ... is not valid; is your activity running?
A check at step three showed, that isFinishing()
is true
the second time. That explains the error. But why is the activity finishing right after getting resumed?
I try to give you the relevant part of the code:
MyActivityA:
public class MyActivityA extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_a);
}
public void sendButtonPressed(View view) {
Intent intent = new Intent(MyActivityA.this, MyActivityB.class);
startActivity(intent);
}
}
MyActivityB:
public class MyActivityB extends AppCompatActivity implements CheckIPResultReceiver.Receiver{
private CheckIPResultReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_b);
mReceiver = new CheckIPResultReceiver(new Handler());
mReceiver.setReceiver(this);
}
@Override
protected void onResume() {
super.onResume();
//starting repeated IntentService via AlarmManager
String targetURL = "www.google.de";
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
alarmIntent.putExtra("targetURL", targetURL);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10000, pendingIntent);
}
public void onReceiveResult(int resultCode, Bundle resultData) {
//getting events from the service
switch (resultCode) {
case SendPostRequest_Service.STATUS_ALERT:
//stopping AlarmManager
Intent alarmIntent = new Intent(MyActivityB.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MyActivityB.this, 0, alarmIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
//debug-check
if(isFinishing()){
// on the 2nd time --> TRUE!
Log.v(TAG, "Act. seems to finish?!");
}
else {
// only on the 1st time true
Log.v(TAG, "Act. seems ok");
new AlertDialog.Builder(MyActivityB.this)
.setTitle("Alert triggered!")
.setMessage("...")
.setCancelable(false)
.setPositiveButton("Restart", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MyActivityB.this, MyActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
break;
}
}
}
Do you have any suggestions? I don't find any reason why MyActivityB
goes into finishing...