Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent opening Activity for multiple times

I have a common menu on my app with icons. Clicking an icon will start an Activity. Is there a way to know if an activity is already running and prevent it from starting multiple times (or from multiple entries)? Also can I bring an activity that is in onPause state to the front?

like image 202
XurajB Avatar asked Sep 06 '25 12:09

XurajB


2 Answers

Use this:

intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

while starting Activity.

from documentation:

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.

like image 126
Adil Soomro Avatar answered Sep 08 '25 20:09

Adil Soomro


In your activity declaration in Manifest file, add the tag android:launchMode="singleInstance"

like image 37
AAnkit Avatar answered Sep 08 '25 19:09

AAnkit