How can i start a new activity from a view class ?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How can i start a new activity from a view class ?

Postby nimou » Wed Jan 06, 2010 4:17 pm

Hello, i would like to know how i can start a new activity from a view class, like something like that :

public class Menu extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new myView(this));

}
}
public class myView extends View{



@Override
public boolean onTouchEvent(MotionEvent event) {
//start a new activity from here for example ??
}
}

thx
nimou
Freshman
Freshman
 
Posts: 4
Joined: Wed Jan 06, 2010 4:13 pm

Top

Postby achie1266 » Wed Jan 06, 2010 6:52 pm

you need to use Intent class and start it with the startActivity method like this.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 Intent intent = new Intent(ThisActivityView.this, NewActivityView.class);
  2.  
  3.                 startActivity(intent);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Postby nimou » Thu Jan 07, 2010 10:27 am

achie1266 wrote:you need to use Intent class and start it with the startActivity method like this.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 Intent intent = new Intent(ThisActivityView.this, NewActivityView.class);
  2.                 startActivity(intent);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


thx for your reply,

actually, i tryed this but i think this method only works from an activity class, not from a view class.. a least not this way...

any other idea ?
nimou
Freshman
Freshman
 
Posts: 4
Joined: Wed Jan 06, 2010 4:13 pm

Postby achie1266 » Thu Jan 07, 2010 11:37 am

what view class. can you post the snippet of your code
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Postby nimou » Thu Jan 07, 2010 2:03 pm

achie1266 wrote:what view class. can you post the snippet of your code

i mean from a gameview for example
but now i understand my mistake...
i didnt understand i had to call it with the context i received from the activity..

if i do it like that its ok :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent intent = new Intent(context, Newactivity.class);
  2. context.startActivity(intent);
  3.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


thx and sry, im just a beginner:)
nimou
Freshman
Freshman
 
Posts: 4
Joined: Wed Jan 06, 2010 4:13 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 7 guests