I’ve implemented a ‘home screen’ for my application, which consists of a gridview containing icons and text. This works fine, and I can add an OnItemClickListener so that tapping an icon will create a toast, for example. But I’m not sure how to call startActivityForResult() from here. I could pass in the application context and use this to create the intent etc, but this doesn’t feel like the right way of doing it.
My code looks like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.title);
GridView gridview = (GridView) findViewById(R.id.icons_gridview);
gridview.setAdapter(new HomeScreenAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
switch(position){
case 0:
//need to start new activity 1 from here
break;
case 1:
//need to start new activity 2 from here
break;
}
}
});
Thanks for any help,
TLB
Method 1 (my prefered method)
Passing ActivityName.this as context is the way I do it. For example
Method 2
You could pass
getApplicationContext()as the context;Method 3
Having a
Context mContextfield is a common method. Set it at the start of youronCreatethen usemContextto start your activities.then
Then you can start a new activity using mContext as the context parameter