To prevent multiple instances from being created, you can do this:
Intent photosIntent = new Intent(Videos.this, Photos.class);
photosIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Videos.this.startActivity(photosIntent);
This will reuse an existing instance of Photos
if that Activity
is currently on top of the stack (ie: visible on screen).
If you have multiple activities, and you want to ensure that you only ever create a single instance of each one, you can do this:
Intent photosIntent = new Intent(Videos.this, Photos.class);
photosIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Videos.this.startActivity(photosIntent);
This will bring any existing instance of Photos
to the top of the stack (ie: visible on screen), even if it is not already on top.
NOTE: Do NOT use the special launch modes singleInstance
or singleTask
as others have suggested. These will not help you and they perform special magic which will likely have you tearing your hair out later. You can specify android:launchMode="singleTop"
for these activities if you want to.