0

I have implemented a download manager which shows the completion notification on the navigation bar and when users touch the notification it automatically lanches my main activity.

Now the problem is when I press back button on main activity, it returns to previous activity which I don't want to happen.

The CODES I have tried are:

on Main Class:

@Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

and this:

@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); 
    }

This is my Download Receiver Code:

private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
        public void onReceive(Context ctxt, Intent i) {
            if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(i.getAction()))
                startActivity(new Intent(getApplicationContext(),MainActivity.class));
            else if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(i.getAction())) {
                Toast.makeText(ctxt,"Done!",Toast.LENGTH_SHORT).show();
                Log.i("download status: ", "Download Completed!");
                MyNotificationManager.ShowNotification((Activity) ctxt, MainActivity.GetTheActivity(), R.drawable.ic_launcher, "package " + downloadedFileName),
                        "package" + downloadedFileName + " is downloaded!"), MyNotificationManager.SoundsType.System);
                Log.i("download status: ", "Notification shown!");

                DoBackgroundDatabaseOperations proceedDownload = new DoBackgroundDatabaseOperations();
                proceedDownload.execute();// Sending the package
            }
        }
    };

This is my MyNotificationManager class which has the following method to show Notification:

public static enum SoundsType
    {
        System,
        My
    }
    public SoundsType SoundType;

    static Uri soundUri;


public static void ShowNotification(Activity curActivity,Activity targetActivity,int icon,String title,String decs,SoundsType sound)
    {
        if (sound == SoundsType.System)
            soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        else if (sound == SoundsType.My)
            soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        Intent notificationIntent = new Intent(curActivity, targetActivity.getClass());  
        PendingIntent contentIntent = PendingIntent.getActivity(curActivity, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);  

        NotificationCompat.Builder builder = new NotificationCompat.Builder(curActivity)  
                .setSmallIcon(icon)  
                .setContentTitle(title)  
                .setContentText(decs)
                .setSound(soundUri)
                .setAutoCancel(true)
                .setContentIntent(contentIntent);

        NotificationManager manager = (NotificationManager) curActivity.getSystemService(Context.NOTIFICATION_SERVICE);  
        manager.notify(0, builder.build());  
    }

Now What should I do in order to handle back button properly so it goes to previous app which could have been running, or home screen if user has launched my app from there?

I know this is more of a question than a code problem but I suppose this is a question of many others...

Nima Sakhtemani
  • 1,119
  • 2
  • 10
  • 21

1 Answers1

0

When you create the notification, you need to set the following flags on the `Intent:

    Intent notificationIntent = new Intent(curActivity, targetActivity.getClass());
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); 

If your application is not already running, this will launch the target activity. If your application is already running and your target activity is still active (ie: not finished) then this will clear all activities in the task that are on top of the target activity and start a new instance of the target activity.

From your description this sounds like what you want.

David Wasser
  • 93,459
  • 16
  • 209
  • 274