How to find out if an action has already begun

I work in an application where I filter when the screen is on or off, and run the action when the screen is on, but once it starts the activity, but the action has already begun, I would like to ask you, it was any way to find out inside my application if activity is already running.

When I run the action, I added this code

intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

Thank you very much.

+4
source share
3 answers

I think you can set a variable with an activity lifecycle

 class IAMActivity extends Activity { static boolean isStart = false; public void onStart() { super.onStart(); isStart = true; } public void onStop() { super.onStop(); isStart = false; } } 
+2
source

Here is what you are looking for:

  class MyActivity extends Activity { static boolean alreadyLaunched = false; @Override public void onStart() { super.onStart(); alreadyLaunched = true; } @Override public void onStop() { super.onStop(); alreadyLaunched = false; } } 
+1
source

If you want to limit your activity to one instance, you can set launchMode in the manifest file as singleTask or singleInstance , depending on your requirements.

 <activity android:launchMode="singleTask" ...> ... </activity> 

With singleTask new intent is delivered onNewIntent() instead of onCreate() .

See <activity> for details.

0
source

Source: https://habr.com/ru/post/1448011/


All Articles