Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 640
Ribs Avatar asked Dec 16 '10 01:12

Ribs


People also ask

How can call one activity method from another activity in Android?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

How do you call a method in MainActivity from another class in Android?

MainActivity.java onCreate(savedInstanceState); setContentView(R. layout. activity_main); instance = this; } public static MainActivity getInstance() { return instance; } public void myMethod() { // do something... } )

How do you call a class in Android activity?

Activity is declared in manifest and has a lifecycle. Instead use startActivity or startActivityForResult . Move your capturing image code to capture_image and use startActivityForResult . Show activity on this post.


1 Answers

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.

like image 178
Cristian Avatar answered Sep 20 '22 02:09

Cristian