I have a single Activity in my app (called MainActivity) and when I send notification to user, I want it to get opened if the notification is clicked. the important point is to avoid activity recreation and if there is already an instance of it alive, bring it to front and deliver the data to it. to get this behavior I have tried:
Intent intent = new Intent(getApplicationContext(), MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
.putExtra("data", ...);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
and in manifest:
<activity
android:name="...activities.MainActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen.Light">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
activity>
but the onNewIntent(Intent intent) method never gets called when I touch the notification, instead onCreate is called and the activity is recreated. please note that since I'm testing, I send the notification from the MainActivity itself and while it is open, I drag status bar and click on my notification (which then recreates the MainActivity). what is the problem?