Reputation: 491
Say I have activityA (launcher activity), and from here I start activityB. At this point activityA is on the activity stack. Now from activityB I call startActivity(new Intent(context, activityA.class)
.
My question is: it creates another instance of activityA or recreates activityA?
Upvotes: 4
Views: 858
Reputation: 95626
To finish B and recreate an instance of A (what you said you wanted to do in the comments), do this:
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
This will finish B and also finish the existing instance of A, and then create a new instance of A.
Upvotes: 2
Reputation: 1702
If you want to use already existing instance of activity and also want to refresh some data in that activity,you no need to recreate that activity ,instead move that refresh data logic into onStart() because onStart() will invoked first not onCreate() when you use existing instance.
first call finish() method to close activity B and avoid new activityA instance creation.
@Override
public void onStart() {
super.onStart();
refreshData();
}
//If you can not call refreshData() in onStart(),try below code
Intent intent=new Intent(ActivityA.this,ActivityB.class);
startActivity(intent);
finish(); //this method will clear ActivityA instance from stack.
then create new instance of Activity A from B using intent.
Upvotes: 1
Reputation: 364
If you want to avoid, that the activityA is launched twice, you can use this flag:
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running.
Upvotes: 1