1

I was wondering what the best way to approach launching activities from other views in a modular way. I'm trying to figure out a way to tell my "button" which activity to fire off once its been selected in the 'onTouchEvent'. Currently, I have a main activity that creates and sets my view to my 'MainMenu'. My main menu defines a MenuItem class that defines a rect for drawing a button, and firing off an activity when intersected/touched/clicked. However, I'm having some difficulty firing off the activity. Below are just a few snippets of code demonstrating some of what I'm trying to achieve:

public class MainMenu extends View {
...
private Vector<MenuItem> menuItems;
private MenuItem testButton;
private MenuItem testButton2;

public MainMenu(Context context) {
    ...
    // Create our menu buttons and load their specific images
    testButton = new MenuItem(context, new OptionsMenu(), 150, 50, imgButtons, 256, 64, 0, 0);
    testButton2 = new MenuItem(context, OptionsMenu.class, 150, 200, imgButtons, 256, 64, 0, 0);

    // Store our buttons
    menuItems = new Vector<MenuItem>(5, 2);
    menuItems.add(testButton);
    menuItems.add(testButton2);
}

...

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() != MotionEvent.ACTION_DOWN)
        super.onTouchEvent(event);

    // Create our menu item iterator 
    Iterator<MenuItem> menuItemsIter = menuItems.iterator();
    Object element;

    // Loop through our menu items, drawing them
    while(menuItemsIter.hasNext()) {
        element = menuItemsIter.next();
        if(((MenuItem)element).HasIntersected((int)event.getX(), (int)event.getY())) {
            ((MenuItem)element).LaunchActivity();
        }
    }

    return true;
}
}

class MenuItem  {
...
private Context container = null;   // Indicates which activity contains us
private Object startObject = null;  // Which activity we'll start/execute

public MenuItem(Context context, Object object, int xPos, int yPos, Bitmap image, 
                int imageWidth, int imageHeight, int xOffset, int yOffset) {
    ...
    container = context;
    startObject = object;
}

... 

public void LaunchActivity() {
    if(startObject != null) {
        Intent activity = new Intent(container, startObject.getClass());
        container.startActivity(activity);
    }
}
}

I tried setting my MenuItem's Object two different ways (new OptionsMenu() and OptionsMenu.class), but neither seem to work. I tried dodging the use of the MenuItem's startObject when created a new Intent, and using (container, optionsMenu.class) for parameters instead. Which didn't work either. From what I know this is the correct way to fire off an activity, but I'm guess I'm missing a step somewhere.

Also, I read a few articles/posts of people mentioning the use of callbacks, but on the Activity side instead of the View side. However, it wasn't very clear if there were built in Android callbacks I should use, or if I should create my own callbacks and setup my own system.

Any information about what I'm doing wrong, or what I could do differently to approach this differently/better would be appreciated. Thanks.

1
  • It seems I just made a novice mistake, and forgot about adding the activities I want to use to my manifest. The correct way to set your Object however, if you want to approach it in a similar fashion is through: "Object myObject = new MyActivity();" then invoking "context.startActivity(new Intent(context, myObject.getClass()));" I'm not sure if they helps anybody, but it was just a mistake on my part. I don't have enough reputation to answer my own question yet :( Oct 4, 2011 at 16:05

1 Answer 1

1

IMHO, just a Button supports sending click events to an OnClickListener, your custom View should have its own custom event interface for sending its own custom events to the controller (e.g., an activity). It is up to the controller to arrange to do something with those events, such as starting up other activities.

1
  • I don't particularly like the stock style of Buttons, so I decided to create my own button. I originally read about the OnClickListener, but it related to the click event for Buttons, as you stated. I'll do a little reading though, and see if I can integrate/implement something related to what you said. I wouldn't mind it working/communicating the way it was intended. Instead of working around the Activity. Then I'll get back with you if you don't mind, so I can verify if I'm going about the situation in a correct fashion. Oct 4, 2011 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.