1

I want to start a notification in current Activity which is ChatActivity. When I press the notification, I want to enter the ChatOneActivity.

However I don't want the current ChatActivity finished when I'm in the ChatOneActivity. Because I'm receiving the data and when I press the back button, I want to stay in the ChatActivity.

The point is I do not want the ChatActivity to finish, no matter which Activity I am currently in.

So what should I do?

Here is the code

private void showNotification(String id, String message) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
    .setSmallIcon(android.R.drawable.sym_action_email)
    .setContentTitle("You have a new message")
    .setContentText(message);
    Intent intent = new Intent(ChatActivity.this,ChatOneActivity.class);
    intent.putExtra("toId", id);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(ChatActivity.this);
    stackBuilder.addParentStack(ChatOneActivity.class);
    stackBuilder.addNextIntent(intent);
    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
    builder.setContentIntent(pendingIntent);
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, builder.build());
}

The problem is:

Now I enter the ChatOneActivityand when I press the back button, I return to the desktop. Means that the ChatActivity has already finished which I don't want.

Here is the mainfest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webmobilegroupchat"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="16" />

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.webmobilegroupchat.MainActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="com.example.webmobilegroupchat.ChatActivity"
        android:label="@string/title_activity_chat" >
    </activity>
    <activity
        android:name="com.example.webmobilegroupchat.ChatOneActivity"
        android:label="@string/title_activity_chat_one" >
    </activity>
    <activity
        android:name="com.example.webmobilegroupchat.SplashActivity"
        android:label="@string/title_activity_splash" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Xiang.G
  • 86
  • 1
  • 7

2 Answers2

1

Try this on ChatOneActivity activity .

  @Override
  public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent(getApplicationContext(),ChatActivity.Class);
    startActivity(intent);
}
h_patel
  • 744
  • 5
  • 16
0

your code is looking fine for creating notification but you have to define parent activity in Manifest also to navigate back, stackBuilder.addParentStack assumes that you define it in Manifest as well for API 16+:

<activity android:name=".ChatOneActivity"
    android:parentActivityName=".ChatActivity"
    ... />

for < API 16:

<activity android:name=".ChatOneActivity">
<meta-data
    android:name="android.support.PARENT_ACTIVITY"
    android:value=".ChatActivity" />

Muhammad Farhan Habib
  • 1,859
  • 20
  • 23
  • Now when I press the back button,it return into ChatActivity. However the ChatActivity has finished.Is there any why can I avoid this?(if not use service) – Xiang.G Aug 30 '16 at 12:04
  • not understand exactly, you want to navigate back to ChatActivity as you mentioned above in your question? – Muhammad Farhan Habib Aug 31 '16 at 06:02
  • i mean your code looks fine .But when i press the notification, onDestory is called.And I don't know why .It means that the ChatActivity is killed by the system – Xiang.G Aug 31 '16 at 08:52
  • It lookes like when you rotatethe screen ,it will invoke onDestory and onCreate .These method are called by the system. And I didn't invoke the finish method – Xiang.G Aug 31 '16 at 08:56
  • yes while changing orientation it could be, you can confirm it by lock the orientation in one mode portrait/landscape & remember to accept the question if its helpful to you – Muhammad Farhan Habib Aug 31 '16 at 09:18
  • but I didn'tchange the orientation...just start an activity – Xiang.G Aug 31 '16 at 09:27
  • you probably need to check this link: http://stackoverflow.com/questions/18425108/how-should-i-do-from-notification-back-to-activity-without-new-intent – Muhammad Farhan Habib Aug 31 '16 at 09:42