2

I have inherited some code hence I don't have true freedom to change it. :(

I have a main activity, from which other activities (I will refer to these as sub activities from now on) are called. Whenever one of these completes, it calls finish and returns data to the main activity.

Each activity (including the main one) has a bar on the top that displays a custom view. The custom view contains a canvas which has a drawing that is dependant upon the state of the network.. i.e. wifi/mobile etc...

Since that 'state' data never changes, it's held within a singleton and the view gets data from the singleton to define what it draws. That is working with no issues, i.e. the data is always as I expect it.

When I first launch the MainActivity, as the network changes, the data changes and each call to 'invalidate' the view receives a system call to 'onDraw' as I would expect.

In each of the sub activities the same is again true.

Upon finishing a sub activity and returning to the mainActivity, calls to invalidate no longer cause a call to onDraw to occur.

I have looked at this for quite a while now and just cannot figure out what is going wrong.

In my constructor I have:

setWillNotDraw(false);

Whenever the data changes the following methods are called:

invalidate();
requestLayout();

Now, there's one more thing... upon returning to the activity at that immediate point, I refresh and this DOES draw correctly, i.e. invalidate does trigger an onDraw call... any subsequent network changes (which are propogated) fail to result in the onDraw call.

I'm wondering if this is to do with the view somehow being detached. I can see that 'onDetachedFromWindow' is called, however the trigger for this is the destruction of the subactivity, hence I don't see why that should affect the MainActivity but it's the only thing I can think of.

I'm hoping I've provided enough information for someone to help me...

greysqrl
  • 937
  • 3
  • 13
  • 31
  • Can you give more detail of the problem? Your subactivity is stopping, but you need to redraw it while it's hiding? Or your main activity stops redrawing after returning from subactivity? – Artem Mostyaev Jan 20 '17 at 13:41
  • Hi Artem, My MainActivity stops redrawing once it returns from the subactivity.... Each SubActivity has its own instance of the view and if it's invalidated from a subactivity the MainActivity refuses to then handle onDraw. – greysqrl Jan 20 '17 at 14:10
  • Actually, I think each view is NOT a unique instance... from a test that seems to be the case. – greysqrl Jan 20 '17 at 14:14
  • Have you some static varibales in your view instance? Or maybe messages sent to the static Handler? – Artem Mostyaev Jan 20 '17 at 14:15
  • Well, what I have now found is that when I return to my main activity, if I reconnect the 'view' in onResume, everything works well. So, now I need to understand exactly why this is the case so I can fix the issue correctly. :) – greysqrl Jan 20 '17 at 14:51
  • Thank you for your help, Artem. :) – greysqrl Jan 20 '17 at 15:11

1 Answers1

1

Well, in the end my answer has very little to do with the question and I guess this is an example of how an issue can be solved by going back to absolute basics and checking for the obvious.

My activities all inherit from an abstract activity. Within that activity there is an instance of the view. The views in which I was having trouble were using that declaration as opposed to having their own instance, hence behaviour from one activity was then affecting another inadvertently.

So, if I'd been able to post up all the code, I'm sure someone else would have spotted this but, unfortunately I couldn't in this instance.

Still, whilst this posting doesn't provide a resolution that will help others, maybe it does say... step back and check the obvious first!

greysqrl
  • 937
  • 3
  • 13
  • 31