0

I have navView and a link on it to another activity each time I click that link it creates a new instance of the activity.

if the user click the link 3 times it creates 3 different instances.

I want to create only one instance at the first time and then reopen

Intent photosIntent = new Intent(Videos.this, Photos.class);
Videos.this.startActivity(photosIntent);
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • declare launch mode for the activity as singleInstance in manifest. `` – Chithra B Apr 02 '18 at 08:44
  • @Chithra Please don't suggest special launch modes to solve this problem! That's not the way to do it. Special launch modes cause more problems than they solve and most developers have no idea how this works. – David Wasser Apr 03 '18 at 14:10

6 Answers6

1

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.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
0

You can check instance for null. If it is null create new, otherwise open old one.

Asset Bekbossynov
  • 337
  • 3
  • 5
  • 16
0

Try this

Intent photosIntent = new Intent(this, Photos.class);
photosIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(photosIntent);
Siddharth Patel
  • 205
  • 1
  • 13
0

This is how Android works. Activities have lifecycles and once you start an Activity a new instance will be created every time. Same as it will be destroyed once you no longer use it (go to another activity, close app, etc.)

try adding android:launchMode="singleTop" to your Android manifest for your activity and see if it works.

bko
  • 1,038
  • 1
  • 9
  • 17
  • you mean override pending transition does not get triggered? – bko Apr 02 '18 at 10:38
  • @Mostafa Try calling `overridePendingTransition()` in `onNewIntent()` see: [link](https://stackoverflow.com/questions/4633543/overridependingtransition-does-not-work-when-flag-activity-reorder-to-front-is-u) – bko Apr 02 '18 at 11:02
0

create a class variable like private Intent photosIntent;

replace this:

Intent photosIntent = new Intent(Videos.this, Photos.class);

with:

if(photosIntent == null){ photosIntent = new Intent(Videos.this, Photos.class); }

this should work fine

Akshay Batra
  • 137
  • 7
-1

You can try using android:launchMode with one of these:

  • android:launchMode="singleTask"
  • android:launchMode="singleTop"
  • android:launchMode="singleInstance"

Here the usage sample:

<activity
    android:name=".YourActivity"
    android:label="activity name"
    android:launchMode="singleTask"
    android:taskAffinity="">

Please be aware the above choices have different characteristic. I think that singleTask is the more suitable for you.

This article: Understand Android Activity's launchMode: standard, singleTop, singleTask and singleInstance explained their characteristics in details.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • I used singleInstance works fine but I lost my sliding animation as i added custom animation in styles.xml – Mostafa Daif Apr 02 '18 at 10:08
  • Use of launch modes `singleInstance` and `singleTask` should rarely be necessary, unless you are building a HOME-screen replacement. Under normal circumstances you do not need these launch modes! Also, setting `taskAffinity` like this will create separate tasks for each `Activity`, which is certainly not what OP wants nor what the user expects. This is the wrong approach to solving this problem. – David Wasser Apr 03 '18 at 14:08