I have multiple activities in our application. One of the activities are starting up activity which is main launcher activity. It starts and initaties few classes which are used by whole application and finishs.
Then there is main activity where user resides most of their time. When notification is created if the application is closed then those classes are destroyed and therefore application gives error (nullpointerexception).
What we need to do is start the launcher activity if application is closed else bring the main activity to front.
I have tried mutliple solutions here.
None of these solutions works. Any help is appreciated.
Update 1:
I though what if we choose which intent should pending intent get by checking if the app is running or not.
public boolean isApplicationRunning(Context context) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (RunningTaskInfo task : tasks) {
if (context.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))
return true;
}
return false;
}
This fixes the problem we have with multiple activities. But this gives another problem, what if user is in main activity and notification is received then user closes application but doesn´t click on notification. Later when user will click on notification application will again give nullpointerexception because at the time of notification was created application was running.
There must be a better way to handle this problem.