Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage lifecycle in a ViewGroup-derived class?

Tags:

android

I had a bunch of code in an activity that displays a running graph of some external data. As the activity code was getting kind of cluttered, I decided to extract this code and create a GraphView class:

public class GraphView extends LinearLayout {     public GraphView(Context context, AttributeSet attrs) {         super(context, attrs);          LayoutInflater inflater = (LayoutInflater)                 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);          inflater.inflate(R.layout.graph_view, this, true);     }      public void start() {         // Perform initialization (bindings, timers, etc) here     }      public void stop() {         // Unbind, destroy timers, yadda yadda     }         .         .         . } 

Moving stuff into this new LinearLayout-derived class was simple. But there was some lifecycle management code associated with creating and destroying timers and event listeners used by this graph (I didn't want this thing polling in the background if the activity was paused, for example).

Coming from a MS Windows background, I kind of expected to find overridable onCreate() and onDestroy() methods or something similar, but I haven't found anything of the sort in LinearLayout (or any of its inherited members). Having to leave all of this initialization code in the Activity, and then having to pass it into the view seemed like it defeated the original purpose of encapsulating all of this code into a reusable view.

I ended up adding two additional public methods to my view: start() and stop(). I make these calls from the activity's onResume() and onPause() methods respectively.

This seems to work, but it feels like I'm using duct tape here. Does anyone know how this is typically done? I feel like I'm missing something...

like image 765
Scott Smith Avatar asked Mar 01 '10 20:03

Scott Smith


People also ask

What is the view lifecycle?

View Life Cycle Every Activity has it's own life cycle similarly Views also have a Life Cycle. A view which was rendered on the screen must undergo these lifecycle methods to get drawn on the screen correctly. Each of these methods has its importance.

What is Life Cycle owner Android?

ProcessLifecycleOwner. Class that provides lifecycle for the whole application process. A class that has an Android lifecycle. These events can be used by custom components to handle lifecycle changes without implementing any code inside the Activity or the Fragment.

What is ViewGroup role?

The ViewGroup class is a subclass of the View class. And also it will act as a base class for layouts and layouts parameters. The ViewGroup will provide an invisible container to hold other Views or ViewGroups and to define the layout properties.

Which of the following is a ViewGroup?

Which of the following is a ViewGroup? A ScrollView is a ViewGroup that can contain any number of Views or ViewGroups as its children.


2 Answers

You may be able to get some use out of overriding protected void onAttachedToWindow() and protected void onDetachedFromWindow() I have never tried, but they may be called approximately when you would like.

like image 50
CaseyB Avatar answered Oct 03 '22 09:10

CaseyB


I've only done a brief experiment with this, but it appears that if you override onAttachedToWindow and onDetachedFromWindow as mentioned by CaseyB above along with overriding

protected void onWindowVisibilityChanged(int visibility)

It should give you the information you need.

I'm running into the same situation as you. I'm surprised there is no notification mechanism for this.

like image 26
ThomasW Avatar answered Oct 03 '22 09:10

ThomasW