1

I am developing android app in which I have 2 activities. I want to close activity A from activity B on button press and recreate activity A. How to do it need help?

Saif Khan
  • 484
  • 1
  • 7
  • 21

1 Answers1

2

You can use sendBroadcast method, by this way you can close one or more activities.

In your ActivityB use this code:

public class ActivityA extends AppCompatActivity {

    public static final String FINISH_ALERT = "finish_alert";    

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);                        

        this.registerReceiver(this.finishAlert, new IntentFilter(FINISH_ALERT));                
    }      

    BroadcastReceiver finishAlert = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            ActivityA.this.finish();
        }
    };

    @Override
    public void onDestroy() {

        super.onDestroy();
        this.unregisterReceiver(finishAlert);
    }
}

and in your ActivityB call this command to finish it:

Intent i = new Intent(ActivityA.FINISH_ALERT);
this.sendBroadcast(i);
javad
  • 833
  • 14
  • 37