mehmet
mehmet

Reputation: 1598

Fired notification from Broadcastreceiver is closing app when backpressed

Well I've an app it has basicly three 3 classes which are 1-MainActivity, 2- DetailActivity and 3-Broadcast. even app was closed broadcastreceiver is working and if it receives something, it fires notification. when I pressed notification it is opening Detail class. until this point there is no problem it is working perfect. but if I press back in DetailActivity. it is directing to me home page of phone. but app should direct me to Main class. after app direct me to home page if I go back to app from background running apps it will start main class I am using docs codes which are:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".DetailActivity"
    android:parentActivityName=".MainActivity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity"/>
</activity>

and

int id = 1;
...
Intent resultIntent = new Intent(this, DetailActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(DetailActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
...
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());

Upvotes: 2

Views: 97

Answers (3)

mehmet
mehmet

Reputation: 1598

Actualy I found the answer after some try and error,

PendingIntent.FLAG_UPDATE_CURRENT

After changing this line it works fine, also instead of ".this" I am using getApplicationContext(); for context

 PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_IMMUTABLE
                );

Upvotes: 1

MeknessiHamida
MeknessiHamida

Reputation: 185

you just need to override it in the detailsActivity ( as per your question's ) : make an intent that directs you to the main Activity:

@Override public void onBackPressed() { //Include the code here return; }

Upvotes: 0

Chathu_sm
Chathu_sm

Reputation: 85

as @MeknessiHamida mentioned..you should override the OnBackPressed method and start a intent to launch your main activity.

Upvotes: 0

Related Questions