Skip to content Skip to sidebar Skip to footer

Is There An Android Intent That Resumes The App Or Opens It Again?

In my android app, this code will open/resume a activity NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

Solution 1:

Ok, here is the hack way I got, maybe someone can improve on it.

public Boolean paused = false;

@Override
protected void onPause() {
    super.onPause();
    paused = true;
}

@Override
protected void onResume() {
    super.onResume();
    paused = false;
}

private void setUpScreen() {
    if (getIntent().getExtras() != null) {
        // ran normally
    } else {
        // opened from notification

        finish();

        // open default activity
        Intent myIntent = new Intent(this, SplashScreen.class);
        startActivity(myIntent);
    }
}

in the async

@Override
protected void onPostExecute(List<CompareResult> compareResults) {


    if (context.paused) {
        showNotification();
    }
}

private void showNotification() {
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notIntent = new Intent(context, SpeciesScreen.class);
    notIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, notIntent, 0);

    Notification noti = new Notification.Builder(context)
    .setContentTitle("Scan Complete")
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentIntent(pIntent)
    .setAutoCancel(true)
    .build();

    notificationManager.notify(0, noti); 
}

Post a Comment for "Is There An Android Intent That Resumes The App Or Opens It Again?"