Reputation: 1181
My application has two activity Activity1 and Activity2. In (Launcher)Activity1, there is map which shows current location and Activity2 has list of tagged locations. When user clicks on particular tag in Activity2, the path is traced on map which is in Activity1. I have to return to Activity1. I tried calling finish()
after tracing path on map
Activity Activity2=(Activity)context;
Activity2.finish();
its working but its not good practice, Please anybody suggest me How can I do this? Thanks in advance
Upvotes: 2
Views: 1805
Reputation: 21883
I don't think there is anything wrong with calling Activity.finish()
to return to the previous Activity - in fact, I'd say it's pretty standard.
A common pattern is to call Activity.startActivityForResult() from the first Activity to launch the second Activity, and then call Activity.setResult() and Activity.finish() on the second Activity to deliver any useful results back to the first.
This is also mentioned in the documentation for Activity.finish()
:
Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().
Even if you are propagating your results via another mechanism (not using setResult
), there is no reason to avoid using finish for this scenario.
Upvotes: 1
Reputation: 427
For switching from one activity to another you can use Intents.
When you are in Activity2 you can use the following code written below.
Intent intent = new Intent(this, Activity1.class);
startActivity(intent);
Upvotes: 1