简体   繁体   中英

Notification start application else bring activity to front

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.

No one is being able to answer my question. So, I was forced to use a solution which is little dirty.

So we will need one more class NotificationActivity .

Wheneven a new notification is received from back-end server. We will send pendingIntent to NotificationActivity.

NotificationCompt.Builder = bla bla bla.
Intent intent = new Intent(context, NotificationActivity.class);
intent.setFlags(FLAG_ACTIVITY_CREATE_NEW_TASK);

PendingIntent pIntent = PendingIntent.startIntent(0, intent, 0, someflags);

In NotificationActivity:

public class NotificationActivity extends Activity {

     @Override
     protected void onCreate(Bundle savedInstances) {
         Intent intent = null;

         try {
             // Call any method from class which is need by Main activity
             someclass.somemethod();

             // If we are here then it means app is running.
             intent = new Intent(context, MainActivity.class);

             // Flag it to bring it to front
             intent.setFlags(FLAG_ACTIVITY_BRING_TO_FRONT);
         } catch (NullPointerException e ) {

             // If we are here then it means that app is closed and 
             // someclass is garbage collected.
             intent = new Intent(context, StartAppActivity.class);

             // Flag it to create new task
             intent.setFlags(FLAG_ACTIVITY_CREATE_NEW_TASK);
         }

         // Start intent, and finish this activity
         startActivity(intent);
         finish();
     }
}

This solves all the problems that i have mentioned above. Don´t know if it creates new problems. If you use this then use a method which has low-cost. This activity will be garbage collected as soon as u finish it. Ok! I lie, in the nearest future.

This would have given problem if u had multiple notifications lying, but android lets you put all notifications into a single pendingIntent so the problem will never arise.

I cannot come up with any other problems .. if u can then tell me or wait until it is tested :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM