2

In Android Studio, I have an activity that calls a view. The user clicks the view, the view continues for sometime, then finishes and sets public boolean finished = true.

Here is the activity that calls the view and its code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    myView = new GameLevelView(this);
    setContentView(myView);
}

In the view, I have this execute:

protected void onDraw(Canvas canvas) {

    //Update canvas elements

    if(cerainCondition == true){
        finished = true;
    }

    try {
        Thread.sleep(10);            // Delay
    } catch (InterruptedException e) { }

    invalidate();  // Force a re-draw
}

This essentially keeps looping. However, I want the original activity to know that this view has set finished = true so that I can start another activity. I do not want to destroy the view because the other activity will be displayed on top.

How would I go about letting the activity know that the view has changed a variable?

I have considered BroadcastReceiver, but I see no way to actually set a broadcast, so I'm not sure if this would work.

John Targaryen
  • 1,109
  • 1
  • 13
  • 29

2 Answers2

2

I believe the answer you are looking for is here. Essentially, you need to create a variable change listener however the author of that document is far more researched in the subject than I.

EDIT

One method to go about your problem is to create a wrapper class to store your current value and track when that value changes as shown here.

In order to track the changes of your wrapper class, you need to create a custom view that implements its traditional constructors with a bonus custom method that will detect value changes.

Then, in your layout.xml file you simply need to add this custom view, which acts like a regular view except is capable of listening for when your boolean value changes.

I suggest reading the post as it is very well written, and I hope it answers your question!

Community
  • 1
  • 1
vkuo
  • 350
  • 1
  • 4
  • 21
  • I don't quite understand his example. He sets up an interface with one unimplemented method, then changes it from the place that the change occurs. How does he access it form the other class that needs to detect if the change has occurred? – John Targaryen Apr 16 '15 at 00:06
  • These are both massive changes to my code, for something as simple as sending a message. Is it possible to use some sort of static variable/method that keeps track of changes everywhere? Also unclear on why i need a wrapper class, an interface listener inside the wrapper class, and a custom view to do all this. Also, how do I even put a view in a layout.xml file when my view is a java class?? – John Targaryen Apr 16 '15 at 02:19
  • What about a "hack", like having an invisible 0x0px button and simulating a click or something like that? – John Targaryen Apr 16 '15 at 02:22
  • How about having both classes implement an interface that has a static variable or method to keep track of the variable? – John Targaryen Apr 16 '15 at 02:37
  • 1
    Sure, I would need to see more code to suggest something helpful. However if you wanted to create a 0px 0px button you most likely could just make sure you set the clickable property false and view.gone as visibility. – vkuo Apr 16 '15 at 03:53
1

You should check the Observer Design Pattern, I think this is what you are looking for.

EDIT:

The idea is to make a kind of implementation of this pattern.

Make a Interface for the listener:

public interface ValueChangeListener { public void valueChanged(boolean newValue); }

Implement this interface in your Activity, the valueChanged(newValue) method will be fired when the value changes. You should start the other activity from inside of it (or whatever action you want to take in response of the changed value).

Create a variable in your custom view of type ValueChangeListener, and when creating the view, just pass the Activity as a parameter (it implements the interface), or create a method (setValueChangeListener(ValueChangeListener listener)), to pass the listener and set it to the before mentioned variable.

From within your view, when you detect the change of the value - just use the variable and call its valueChanged(newValue) method… this will fire the ‘onValueChanged(newValue)’ in your Activity.

ivtoto
  • 241
  • 1
  • 9