Search code examples
javaandroidandroid-activityandroid-notifications

How to properly add new Activity onto top of the stack after tapping notification


I'm writing a really big app with plenty of activities. I have one class running in the background that is checking are there any changes on the server and perform appropriate action. One of the actions is to send notification to user about employee's clock in, out and similar. That kind of notification is clickable, and it should open employee's contact page PeopleSingleScene_Activity, and that is working as expected. However, when I click back button, application quits (no parent activities).

Code for creating notification with pending intent is as following:

public void sendEmployeeWorkNotification(String ticker, String title, String text, String loc, int employee_id) {
    PendingIntent pendingIntent;
    Intent intent;
    Context currentContext = G.context;

    if (loc.equals("LOC")) {
        intent = new Intent(currentContext, PeopleSingleScene_Activity.class);
        intent.putExtra("people_id", employee_id);
        pendingIntent = PendingIntent.getActivity(currentContext, 0, intent, 0);
    } else {
        //some other actions
    }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(currentContext, CHANNEL_ID)
        .setSmallIcon(R.drawable.vector_notif)
        .setTicker(ticker)
        .setContentTitle(title)
        .setContentText(text)
        .setColor(ContextCompat.getColor(currentContext, R.color.rcOutcome))
        .setStyle(new NotificationCompat.BigTextStyle()
                .bigText(text))
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(currentContext);

    notificationManager.notify(G.simpleMessageCounter, builder.build());
    simpleSong(R.raw.notificationsimple);
    G.simpleMessageCounter++;
}

G.context is the last activity context opened... It is a static variable in global class G. I'm creating it in a way:

public class SomeActivityClass extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.some_layout);
        G.context = this;
        ....
    }
    ....
}

I have also tried with context from my App class for retrieving string values or anything else with a context in classes without activities ...

public class App extends Application {
    private static Context mContext;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
    }

    public static Context getContext(){
        return mContext;
    }

    public static String getStr(int resid) {
        return mContext.getResources().getString(resid);
    }
}

When I use this context from App.getContext(); result is the same, after tapping back button, application quits.

Question is: Is there a problem with a context for creating pending intent and notification, or there is an issue with intent creation flags? I have tried lot of combinations with contexts and flags, but none of them works. And I cannot write (or I don't know maybe) some listeners on each activity to listen for this kind of event and open new intent from itself. There will be over 50 activities...


Solution

  • Finally found a solution, thanks to my buddy Miljan.

    When creating pendingIntent, just needed a proper flag. So instead of

    pendingIntent = PendingIntent.getActivity(currentContext, 0, intent, 0);

    just put a flag

    pendingIntent = PendingIntent.getActivity(currentContext, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    and it works regularly. I can go back to previous activity after displaying activity intitiated by tapping notification.