I have recently implemented notifications for my application. What I have been testing is the behavior of how the application should work. I would imagine it would be something like this:
Notification is received and open Comments.class
through intent -> MainActivity.class
-> click back (exit application, go to home screen of Android device) -> reopen application through Task Manager -> Go back to Comments.class
The first few steps work but when I go from the task manager to open back up the application, I notice it goes to Comments.class
which seems like expected behavior as I have tested with other Android applications.
However, some of the properties of my Comments.class
are not being set and I know it is because the onCreate
method of my Comments.class
is not being called. Is there any reason why this wouldn't happen? This was my attempt:
NotificationService.java
:
Intent resultIntent = new Intent(context, Comments.class);
Intent backIntent = new Intent(context, MainActivity.class);
backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//Set a random notification Id for each notification so that if you get multiple, the first does not get replaced.
Random random = new Random();
int notificationId = random.nextInt(9999 - 1000) + 1000;
PendingIntent pendingIntent = PendingIntent.getActivities(context, notificationId, new Intent[] { backIntent, resultIntent}, PendingIntent.FLAG_ONE_SHOT);
//When the notification is actually clicked on
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notificationBuilder.build());
This first creates the notification and notifies the user of the application. Then Comments.java
's onCreate
function is called which is expected as I click on the Notification
. Then, I click back which simply navigates back to MainActivity
like so:
Comment.java
:
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
Then, MainActivity.java
is called and created which is all fine. If I click back on MainActivity.java
, then I perform the following code:
Here's from MainActivity.java
@Override
public void onBackPressed() {
super.onBackPressed();
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
Which takes me back to my device's home screen. However, an instance of my application is still running in the background and when I click on my application in the background and put it into the foreground, I am navigated back to Comments.java
but because Comments.java
's onCreate
method is not called, some of the details of the Activity
is not initialized. Any idea why this is happening?
Any help would be appreciated. Thanks!
Put your initializations inside onResume
function of your Comment.java
. Here's a good read about Android Activity Lifecycle which may help you to understand the behaviour correctly.
@Override
public void onResume() {
// Your initialization
}