Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Activity finish() results black screen

I have an AlertActivity and an Activity. When a broadcast is received, both activities needs to finish. But the below code results Black screen if AlertActivity is on top of Activity.

Below is the code in Activity:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("BROADCAST_INTENT")){
           if(alertActvity != null)
               alertActivity.finish();
           finish();
    }
}

And code in AlertActivity:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("BROADCAST_INTENT"))
           finish();
    }
}

First, Activity's onStop() is getting called before AlertActivity's onStop() is called which results in Black screen, even AlertActivity's finish() called before Activity's finish().

Please help me in this regard.

like image 748
Avadhani Y Avatar asked Sep 19 '25 06:09

Avadhani Y


1 Answers

Finally, I found a solution for this:

Finishing an Activity with a delay of 1 second which really works. By that time, AlertActivity finishes and black screen cannot be displayed.

new Handler().postDelayed(new Runnable() {
      @Override
          public void run() {
              finish();
          }
}, 1000);
like image 72
Avadhani Y Avatar answered Sep 20 '25 21:09

Avadhani Y