Ribs
Ribs

Reputation: 1505

How can I call a function in my main Activity class from a custom Gallery view in Android?

I have a custom gallery view in which I am overriding some methods. I would like to be able to call a function in my main activity from this class. How do I make a reference back to my main class?

I thought I'd just push the class reference into CustomGallery by creating a setter function ---> g.setBaseClass(this);

CustomGallery g = (CustomGallery) findViewById(R.id.playSelectionGallery);
g.setSpacing(10);
g.setCallbackDuringFling(false);
g.setAdapter(new ImageAdapter(this));
g.setSelection(1);
registerForContextMenu(g);
g.setBaseClass(this);

Problem is this is of type Context and someFunctionToCall() will result in a not a member of this class error. In my custom class I have:

public void setBaseClass(Context baseClass)
{
    _baseClass = baseClass;
}
private void callSomeFuntionOnMyMainActivityClass()
{
    _baseClass.someFunctionToCall();
}

All I want to do is call back to my main class, called ViewFlipperDemo. This would be easy in As3. Any thoughts? Hopefully I'm missing something really simple.

Upvotes: 6

Views: 5247

Answers (1)

Cristian
Cristian

Reputation: 200150

That's actually not a good idea... but you can do it this way:

private void callSomeFuntionOnMyMainActivityClass()
{
    ((ViewFlipperDemo)_baseClass).someFunctionToCall();
}

What you should do instead is implementing a simple observer which allows you to notify the Activity that something happened. That's one of the main OO principles, your custom class shouldn't know anything about your activity class.

Observer pattern example

The Observer interface:

// TheObserver.java
public interface TheObserver{
    void callback();
}

Your custom view:

public class CustomGallery{
    private TheObserver mObserver;

    // the rest of your class

    // this is to set the observer
    public void setObserver(TheObserver observer){
        mObserver = observer;
    }

    // here be the magic
    private void callSomeFuntionOnMyMainActivityClass(){
        if( mObserver != null ){
            mObserver.callback();
        }
    }
    // actually, callSomeFuntionOnMyMainActivityClass
    // is not a good name... but it will work for the example
 
}

This is the activity that will benefit of the observer (notice that now you can use your custom view on different activities not just one, that's one of the key reasons to implement it this way):

public class YourActivity extends Activity{
    // your normal stuff bla blah

    public void someMethod(){
        CustomGallery g=(CustomGallery)findViewById(R.id.playSelectionGallery);
        g.setObserver(new TheObserver(){
            public void callback(){
                // here you call something inside your activity, for instance
                methodOnYourActivity();
            }
        });
    }
}

You will notice that this design pattern (observer) is widely used in Java and Android... almost any kind of UI event is implemented using observers (OnClickListener, OnKeyListener, etc.). By the way, I didn't test the code, but it should work.

Upvotes: 15

Related Questions