Search code examples
androidandroid-activitynotifications

after onClick() notification opens customactivity, I can't get back to mainactivity


In my app I have MainActivity and let's say another CustomActivity. Normally my app starts with mainActivity, that's ok.

But I am sending a firebase notifications, and what I want is to open CustomActivity after I click the notification.

I think I've managed this, because it works:

  if (getIntent().getExtras() != null) {
        startActivity(new Intent(getApplicationContext() , CustomActivity.class));
        finish();
    }

So when a notification is tapped, it will launch CustomActivity.

However, in my CustomActivity I have a back arrow on the top, and of course it will not work, because CustomActivity was my start activity and the app doesn't know where to return step back...

Please what to do with this? I want to bring the user back to MainActivity if he clicks the back arrow.

The arrow is working if the notification is tapped when the app is running, because the MainActivity lifecycle is still on, but the arrow works not when the app is closed and customActivity is iniciated as first.


Solution

  • If you redirecting to CustomActivity trough the code you posted from the MainActivity, you can simply DO NOT finish() so it get to the activity stack below CustomActivity and the back arrow work as indeed.