Below is my sendNotification function, which will be called from the Service, it works fine by creating a notification and displaying it in the status bar. But when the application is already running (I'm in MainActivity), by clicking on the notification to open ConversActivity, the parent MainActivity will be destroyed, and when im clicks the return button from ConversActivity, MainActivity is recreated. Please help me avoid this behavior when the application is running (either in focus / background)
Btw, this is the correct behavior when the application is killed or does not work, due to which MainActivity is created from the backstack, which is correct. But when the application is running, why is MainActivity destroyed and recreated? This is inefficient and causes the application to become slow due to launching the application from MainActivity onCreate.
Thanks in advance and best regards.
AndroidManifest.xml:
<activity android:name=".View.MainActivity" android:hardwareAccelerated="true" android:windowSoftInputMode="stateHidden" android:launchMode="singleTop" android:label="@string/app_name" android:screenOrientation="portrait" android:theme="@android:style/Theme.Black.NoTitleBar" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".View.ConversActivity" android:label="@string/title_activity_convers" android:parentActivityName=".View.MainActivity" android:screenOrientation="portrait" android:windowSoftInputMode="stateHidden|adjustResize" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".View.MainActivity"/> </activity>
-
ConversActivity.java (return button):
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. switch (item.getItemId()) { case android.R.id.home: //onBackPressed(); //finish(); NavUtils.navigateUpFromSameTask(this); return true; case R.id.action_settings: return true; } return super.onOptionsItemSelected(item); }
-
public static void sendNotification(Context context, String dname) { NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Intent intent = new Intent(context, ConversActivity.class); intent.putExtra("dname", dname); PendingIntent pendingIntent = TaskStackBuilder.create(context) // add all of DetailsActivity parents to the stack, // followed by DetailsActivity itself .addNextIntentWithParentStack(intent) .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("You've got a message!") .setStyle(new NotificationCompat.BigTextStyle().bigText("@" + dname + ": " + msg)) .setContentText("@" + dname + ": " + msg) .setAutoCancel(true) .setVibrate(new long[]{100, 100}) //off-on-off-on-off .setLights(Color.GREEN, 3000, 3000) .setSound(Settings.System.DEFAULT_NOTIFICATION_URI); ; mBuilder.setContentIntent(pendingIntent); mNotificationManager.notify(dname, 0, mBuilder.build()); }