Search code examples
androidandroid-intentandroid-pendingintentandroid-popupwindow

How to Handle Pending Intent on Notification Progress bar?


In my app I am uploading an image to a server,a notification progress bar is there to show upload progress.when a user click on the notification ,a popup window will show to cancel or continue upload.All these are working fine.When I click back button and when I am at previous activity,if I click notification the popup window will not be displayed instead it only load the Activity for file Upload.How can I solve this issue??.When user clicks on the notification a pending Intent will be passed and at onNewIntent method I am calling popup window method.This is working fine when I am on the file upload activity.I don't know what is the actual problem.I think the pending intent is deals with another intent in my onCreate method, Can anyone help??

Pending Intent on Notification progress

 public void ProgressBarNotification() {
            mBuilder = new NotificationCompat.Builder(ImageUploadActivity.this);
            mBuilder.setContentTitle("Upload")
                        .setContentText("Upload in progress")
                        .setSmallIcon(R.drawable.ic_launcher);

  mBuilder.setProgress(100, 0, false);

    mBuilder.setAutoCancel(true);
  Intent myIntent = new Intent(this, ImageUploadActivity.class);  //Pending Intent
  myIntent.putExtra("first_state",3);
  PendingIntent pendingIntent = PendingIntent.getActivity(ImageUploadActivity.this, 1, myIntent, PendingIntent. FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(pendingIntent);
    mNotifyManager.notify(MY_NOTIFICATION_ID, mBuilder.build());
}

onNewIntent method

 @Override
 protected void onNewIntent(Intent intent) {
   super.onNewIntent(intent);
    //new Intent(this, ImageUploadActivity.class);
   Bundle extras = intent.getExtras();
   state = extras.getInt("first_state");

      if(state==3)
  { 
   initiatePopupWindow();
   }}

InitiatePopupwindow method

 public void initiatePopupWindow() {
    try {

        LayoutInflater inflater = (LayoutInflater) ImageUploadActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.popupmenu,
                (ViewGroup) findViewById(R.id.popup_element));
        pwindo = new PopupWindow(layout, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
        pwindo.setFocusable(true);
        pwindo.setOutsideTouchable(true);
        Button btnStopUpload = (Button) layout.findViewById(R.id.btn_upload);
        btnStopUpload.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                mNotifyManager.cancel(MY_NOTIFICATION_ID);
                //  mTask.cancel(true);
                Log.e(TAG, "Notification Cancelled ");
                // mTask.cancel(true);
                Toast.makeText(getApplicationContext(),
                        "Upload Cancelled", Toast.LENGTH_SHORT).show();
                ImageUploadActivity.this.finish();
            }
        });


        Button btnCancelPopup = (Button) layout.findViewById(R.id.btn_cancel);

        btnCancelPopup.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                pwindo.dismiss();

            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private View.OnClickListener cancel_button_click_listener = new View.OnClickListener() {
    public void onClick(View v) {
        pwindo.dismiss();

    }
};

Solution

  • On pressing back from ImageUploadActivity it has been removed from the activity backstack so when the user click on notification the pendingIntent create a new instance of the activity.OnNewIntent() always get called for singleTop/Task activities except for the first time when activity is created. At that time onCreate is called.

    In onCreate() of ImageUploadActivity you can call onNewIntent(getIntent()), In onNewIntent() check if extras isn't null and contains first_state.