简体   繁体   English

Android:如果从通知栏恢复应用,则绘制的画布上的颜料将消失

[英]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 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 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. 但是,从通知栏继续,所有行都消失了,并重新启动绘图板,再次显示onCreate_NewDialog。

Why the drawn lines are gone if resuming from notification bar but still there if resuming from long-pressing home screen? 如果从通知栏恢复,为什么绘制的线条消失了,而从长按主屏幕恢复,为什么绘制线条仍然消失了?

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. 按下主屏幕按钮并从长按主屏幕恢复,线条仍然存在。

Good. 好。

However, resuming from the notificaton bar, all the lines are gone and the drawing plate is restarted all over again showing the onCreate_NewDialog. 但是,从通知栏继续,所有行都消失了,并重新启动绘图板,再次显示onCreate_NewDialog。

This seems to be happening due the creation flag you are using: notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 这似乎是由于您正在使用创建标记而发生的: notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); , as documentation states: ,如文档所述:

If set, this activity will become the start of a new task on this history stack. 如果设置,此活动将成为此历史记录堆栈上新任务的开始。

You could try other flags combination, for example, notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 您可以尝试其他标志组合,例如, notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); but be aware that this will change the "normal" back stack behaviour. 但是请注意,这将改变“正常”的后堆栈行为。

In addition to this, it is possible you need to handle the current state of your app. 除此之外,您可能还需要处理应用程序的当前状态。 Try to enable "Don't keep activities" under developer settings then try to resume your app through the activities history. 尝试在开发人员设置下启用“不保留活动”,然后尝试通过活动历史记录恢复您的应用。

I think the problem is in the for loop in onDraw - probably paths, colorsMap and widthMap are empty when you recreate your view. 我认为问题出在onDraw的for循环中-当您重新创建视图时,路径,colorsMap和widthMap可能为空。 When and where do you set their data? 您何时何地设置他们的数据?

Either way, this is not a problem with the notifications - this is a problem with your data persistence. 无论哪种方式,这都不是通知的问题-这是数据持久性的问题。 The same bug can probably be observed if you check the option "Don't keep activities" in the device's Developer Options. 如果您在设备的“开发人员选项”中选中“不保留活动”选项,则可能会观察到相同的错误。

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. 现在,您从历史记录中调用活动,而不是创建新的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM