title: Android activity multiple instance
body: <p>I'm working with React Native where I'm trying to create an independent screen that should be called when the device is locked and the app is closed.</p>
  
  <p>Following is my code:</p>
  
  <p><strong>UnlockScreenActivity.java</strong></p>
  
  <pre><code>public class UnlockScreenActivity extends ReactActivity {
  // public class UnlockScreenActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
          | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
  
          setContentView(R.layout...); // the xml layout
  
          final ReactContext reactContext = getReactInstanceManager().getCurrentReactContext();
  
          Button openBtn = (Button) findViewById(R.id.accept_call_btn);
          openBtn.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View view) {
                  ...
              }
          });
  
          Button closeBtn = (Button) findViewById(R.id.reject_call_btn);
          closeBtn.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View view) {
                  ...
                  finish();
              }
          });
      }
  
      private void sendEvent(ReactContext reactContext, String eventName, WritableMap params) {
          reactContext
          .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
          .emit(eventName, params);
      }
  }
  </code></pre>
  
  <p><strong>AndroidManifest.xml</strong>:</p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      ...
      &lt;application
          android:name=".MainApplication"
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:launchMode="singleTop"
          android:theme="@style/AppTheme"&gt;
          &lt;activity
              android:name=".MainActivity"
              android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
              android:label="@string/app_name"
              android:windowSoftInputMode="adjustResize"&gt;
              ...
          &lt;/activity&gt;
          ...
          &lt;!-- none of this worked --&gt;
          &lt;!-- android:launchMode="singleInstance" --&gt;
          &lt;!-- android:launchMode="singleTask" --&gt;
          &lt;activity android:name=".UnlockScreenActivity" android:noHistory="true" /&gt;
          ...
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>How I call my screen:</strong></p>
  
  <pre><code>Intent i = new Intent(context, UnlockScreenActivity.class);
  i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
  i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
  i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); // still the same
  i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // still the same
  i.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); // still the same
  startActivity(i);
  </code></pre>
  
  <p>After the screen opens, I see that the app has two instances:</p>
  
  <p><a href="https://i.stack.imgur.com/Ubl2q.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ubl2q.jpg" alt="enter image description here"></a></p>
  
  <p>How do I get it in a single instance?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>This is how I manage and so far it's working</p>
  
  <pre><code>// ReactApplicationContext context
  Intent i = new Intent(context, UnlockScreenActivity.class);
  i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
  i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
  i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  context.startActivity(i);
  </code></pre>
  
  <p><strong>AndroidManifest.xml</strong>:</p>
  
  <pre><code>&lt;activity
      android:name=".UnlockScreenActivity"
      android:label="@string/app_name"
      android:launchMode="singleTop"
      android:persistableMode="persistNever"
      android:autoRemoveFromRecents="true" /&gt;
  </code></pre>