Finish an activity from another activity
That you can do, but I think you should not break the normal flow of activity. If you want to finish you activity then you can simply send a broadcast from your activity B to activity A.
Create a broadcast receiver before starting your activity B:
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent intent) {
String action = intent.getAction();
if (action.equals("finish_activity")) {
finish();
// DO WHATEVER YOU WANT.
}
}
};
registerReceiver(broadcastReceiver, new IntentFilter("finish_activity"));
Send broadcast from activity B to activity A when you want to finish activity A from B
Intent intent = new Intent("finish_activity");
sendBroadcast(intent);
I hope it will work for you...
Make your activity A in manifest file:
launchMode = "singleInstance"
When the user clicks new, do
FirstActivity.fa.finish();
and call the new Intent.When the user clicks modify, call the new Intent or simply finish activity B.
FIRST WAY
In your first activity, declare one Activity object
like this,
public static Activity fa;
onCreate()
{
fa = this;
}
now use that object in another Activity
to finish first-activity like this,
onCreate()
{
FirstActivity.fa.finish();
}
SECOND WAY
While calling your activity FirstActivity
which you want to finish as soon as you move on,
You can add flag while calling FirstActivity
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
But using this flag the activity will get finished evenif you want it not to. and sometime onBack
if you want to show the FirstActivity
you will have to call it using intent.
This is a fairly standard communication question. One approach would be to use a ResultReceiver in Activity A:
Intent GotoB=new Intent(A.this,B.class);
GotoB.putExtra("finisher", new ResultReceiver(null) {
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
A.this.finish();
}
});
startActivityForResult(GotoB,1);
and then in Activity B you can just finish it on demand like so:
((ResultReceiver)getIntent().getExtra("finisher")).send(1, new Bundle());
Try something like that.