安卓:从通知栏恢复应用时,绘制的画布涂料会消失

8

我正在开发一款绘图应用程序,并希望通过通知栏恢复活动状态。经过研究,我已经添加了以下代码来从通知栏中恢复(而不是创建)。

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

问题:

绘图板使用颜料可以进行绘画。按下主页按钮并从长按主屏幕恢复时,线条仍然存在。但是,从通知栏恢复时,所有线条都消失了,并且绘图板重新启动,显示onCreate_NewDialog。

为什么从通知栏恢复时绘制的线条会消失,而从长按主屏幕恢复时线条仍然存在?

3个回答

0

替换:

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

作者:

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);

现在,您可以从历史记录中调用活动而不是创建新类。

0

我认为问题出在onDraw方法的for循环中,可能是因为当您重新创建视图时,paths、colorsMap和widthMap为空。您何时以及在哪里设置它们的数据?

无论如何,这不是通知的问题,而是与您的数据持久性有关。如果您在设备的开发者选项中选中“不保留活动”选项,则可以观察到相同的错误。


0
绘画板可以使用颜料进行绘制。按下主页按钮并从长按主屏幕恢复,线条仍然存在。
很好。
然而,从通知栏恢复时,所有线条都消失了,并且绘图板重新启动,显示onCreate_NewDialog。
这似乎是由您使用的创建标志引起的:notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 如documentation所述:
如果设置,则此活动将成为此历史记录堆栈上新任务的开始。
您可以尝试其他标志组合,例如notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 但请注意,这将更改“正常”的后退堆栈行为。
除此之外,您可能需要处理应用程序的当前状态。尝试在开发人员设置下启用“不保留活动”,然后尝试通过活动历史记录恢复应用程序。

谢谢您的回复。我已经尝试了notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);,但它会在旧活动上创建一个新的活动,并需要按返回按钮来关闭新的活动以显示绘制路径,但画布的背景也消失了。 - pearmak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接