Search code examples
androidandroid-notificationsonresume

Android: Drawn canvas paint gone if resuming app from notification bar


I am working on a drawing app and would like to resume the activity from the notification bar. I have added in codes from resuming (instead of creating) from notification bar after research as follows.

Doodlz2

@Override
   protected void onCreate(Bundle savedInstanceState) 
   {
      super.onCreate(savedInstanceState);
      this.requestWindowFeature(Window.FEATURE_NO_TITLE);       
      setContentView(R.layout.main); // inflate the layout    
      generateNotification(Doodlz2.this, getResources().getString(R.string.app_name));

      Display display = getWindowManager().getDefaultDisplay(); 
      Constants.SCREEN_W = display.getWidth();  // deprecated
      Constants.SCREEN_H = display.getHeight();  // deprecated

      doodleView = (DoodleView2) findViewById(R.id.doodleView);
      doodleView.setOnTouchListener(this);    
      doodleView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() 
      {
            @Override
            public void onGlobalLayout() 
            {
                Constants.DRAW_W = doodleView.getWidth(); 
                Constants.DRAW_H = doodleView.getHeight();
            }
      }); 
      OnCreate_NewDialog();
   }

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

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

   @Override
   protected void onDestroy() 
   {
       super.onDestroy();
   }

private static void generateNotification(Context context, String message)
   {
        Intent notificationIntent = new Intent(context, Doodlz2.class);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.logo)
            .setContentTitle(context.getString(R.string.app_name))
            .setContentIntent(intent)
            .setPriority(5) //private static final PRIORITY_HIGH = 5;
            .setContentText(message)
            .setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_LIGHTS);
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, mBuilder.build());
    }

DoodleView2

public DoodleView2(Context c, AttributeSet attrs) 
   {
       super(c, attrs);
       context_new = c;    
       setFocusable(true);
       setFocusableInTouchMode(true);
       //setLayerType(View.LAYER_TYPE_SOFTWARE, null); // for solely removing the black eraser

       mPath = new Path(); 
       mCanvas = new Canvas();  
       mBitmapPaint = new Paint(Paint.DITHER_FLAG);   

       Constants.mPaint = new Paint();
       Constants.mPaint.setAntiAlias(true); // smooth edges of drawn line
       Constants.mPaint.setDither(true);
       Constants.mPaint.setColor(Color.BLACK); // default color is black
       Constants.mPaint.setStyle(Paint.Style.STROKE); // solid line
       Constants.mPaint.setStrokeJoin(Paint.Join.ROUND);
       Constants.mPaint.setStrokeWidth(5); // set the default line width
       Constants.mPaint.setStrokeCap(Paint.Cap.ROUND); // rounded line ends 
   } 

   @Override
   public void onSizeChanged(int w, int h, int oldW, int oldH)
   {
       super.onSizeChanged(w, h, oldW, oldH);
       mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
       mBitmap = getResizedBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.bdg6),h,w); 
   }   

   @Override
   protected void onDraw(Canvas canvas) 
   {
       canvas.drawBitmap(mBitmap, 0, 0, null); // draw the background screen

       for (Path p : paths)
       {
           Constants.mPaint.setColor(colorsMap.get(p));
           Constants.mPaint.setStrokeWidth(widthMap.get(p));
           canvas.drawPath(p, Constants.mPaint);           
       }       
       Constants.mPaint.setColor(selectedColor);
       Constants.mPaint.setStrokeWidth(selectedWidth);
       canvas.drawPath(mPath, Constants.mPaint);                   
   } 

Question:

The drawing plate is working with paints can be drawn onto. Pressing the home button and resuming from long pressinghome screen, the lines are still there. However, resuming from the notificaton bar, all the lines are gone and the drawing plate is restarted all over again showing the onCreate_NewDialog.

Why the drawn lines are gone if resuming from notification bar but still there if resuming from long-pressing home screen?


Solution

  • Replace:

    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    

    by:

    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
    

    Now, you call the activity from the history instead of create new class.