2

Is there a way to start an activity from a view? I have this nice view that renders these menu buttons and when they click a particular button (Trapped in an onTouch event) I want to start the "clicked" activity.

It seems that there from my view startActivity method is uncallable. So that leads me to believe that there is no way to do this, in which case I guess I am asking, what do I do here when I want this menu to have a view that animates drawables and can get touches to know what "button" was pressed to know what activity to start?

I have been searching and nothing seems to fit what I am looking for. I suspect since I cannot find it that it probably breaks convention in some way and in which case, what is the proper way to do this?

I have another Activity that starts this cool scrolling tile map view. When the user clicks on a specific tile I would like to load up an entirely different view (maybe even activity not sure which I need) based on the tileID clicked. This new View or activity would show all the items in the cell that was clicked (imagine the map with cells , each cell has stuff in it. When the user clicks on the cell then a new view happens that shows all this stuff as cool drawables, like it "zoomed in" on the cell).

I would think I need the same thing, when the user is inside my scrolling map view and they push down then up I get what cell they clicked on and use that to initialize the activity that will launch showing the zoomed in state of things (via drawables using a new kind of view i suspect)

Codejoy
  • 3,722
  • 13
  • 59
  • 99

2 Answers2

4

Is there a way to start an activity from a view?

Arguably, the View should tell the Activity that hosts it to start an activity, via a registered callback method. I am not a fan of the code organization you have described here (but, admittedly, based solely on the description).

However, if you really want to have the View start the activity, have the View call getContext().startActivity().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks CommonsWare you are probably correct. I would love to have my view say "oh this tile was touched, okay activity I'm from start up another" instead of the view launching the activity. It feels tighter that way... registered call back, I will have to research those I know nothing of those. – Codejoy Jun 19 '10 at 05:11
  • I did some more research, I can figure out I think how to have one activity start another using the: startActivityForResult what I cannot seem to find though is how to call back to the host activity of a view Seems to me if i can do it that way I can have many views under one activity (and they could maybe even easily share each other with each of themselves (the views) and with main activity. So I just cannot figure out how to say in a view when the right kind of ontouch event is fired,the caling activity gets the callback, parses what the onTouch did and responds by setting up a nw view – Codejoy Jun 19 '10 at 09:10
  • @Codejoy: "what I cannot seem to find though is how to call back to the host activity of a view" -- See http://github.com/commonsguy/cw-advandroid/tree/master/Views/ColorMixer/ for an example of a custom `View`, with the `Activity` registering a listener object. It's pretty much just Java, nothing really Android-specific. – CommonsWare Jun 19 '10 at 12:06
-1

you do start an activity using the startActivity.

// first create the intent
Intent intent = new Intent ( this,  ClickedActivity.class );
// start the activity
this.startActivity( intent );
Ryan Conrad
  • 6,870
  • 2
  • 36
  • 36