I have this scenario:
Main activity with one button. Once clicked it launchs 'mapActivity'
Map activity (extending MapActivity) uses an ItemizedOverlay (extending ItemizedOverlay) class to mark places over the map.
Now, I want to launch another activity from my itemized overlay. I achieved to launch one activity as standar intent this way:
#Override
public boolean onTap(GeoPoint point, MapView mapView) {
boolean tapped = super.onTap(point, mapView);
if (!tapped){
Intent intent = new Intent();
intent.setClass(this.context, EditarLugarActivity.class);
this.context.startActivity (intent);
return true;
}
EditarLugarActivity is an activity whichs obtain a text input from user. Now, I need to get that text from the Itemized Overlay activity. Context have the value from map activity context and is set in constructor in this way:
public MiItemizedOverlay(Context context, Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
this.context = context;
populate();
}
To do this I've tried to use 'sartActivityForResult' instead 'this.content.startActivity(intent) in this way:
#Override
public boolean onTap(GeoPoint point, MapView mapView) {
boolean tapped = super.onTap(point, mapView);
if (!tapped){
Intent intent = new Intent();
intent.setClass(this.context, EditarLugarActivity.class);
this.context.startActivityForResult (intent,1);
return true;
}
but is not recognized by the compiler. 'this.context' is not working as it work for a simple 'startActivity'
Someone can help me with this? Thanks!
EDIT-
Adding my MiItemizedOverlay class first line with 'extends':
public class MiItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context context; [....]
you can try
Intent intent = new Intent();
intent.setClass(getApplicationContext, EditarLugarActivity.class);
this.context.startActivityForResult (intent);
or
Intent i = new Intent(this, EditarLugarActivity.class);
startActivityForResult(i, 1);
There is no no method called startActivityForResult which takes only one argument.
Use, context.startActivityForResult(intent, 1); Here the second argument is the result code which would be returned back to you from EditarLugarActivity when finish() is called on that activity. You can define your own result code. And then you need to implement onActivityResult(int requestCode, int resultCode, Intent data) in your activity to catch this result code.
EDIT:
StartActivityForResult method is from Activity class. So you need to call it as below
((Activity) this.context).startActivityForResult(intent, 1);
Make sure that your context is indeed an Activity context, and implement the onActivtyResult in the mapActivity.
Related
Fairly new to Android. I've the following scenario:
App starts in Main Activity and here I add a fragment
In that fragment is a listview populated with a custom adapter, I am passing an instance of the FragmentActivity to the adapter.
Call:
ExpandableListAdapter listAdapter new ExpandableListAdapter(mContext, mActivity, listDataHeader, listDataChild);
Implementation:
public ExpandableListAdapter(Context context, FragmentActivity activity, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
this._context = context;
this._activity = activity;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
When you click an item in the list, it opens a dialog with edittext fields namely: Name, Surname, Cell. I am passing the FragmentActivity to the dialog as the context:
txtListChild.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new SelectContactDialog(_activity);
dialog.show();
}
});
Then when you focus any of the 3 fields a Select contact opens up, I am using the passed FragmentActivity to call startActivityForResult like so:
private class ContactListener extends Activity implements View.OnFocusChangeListener
{
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
_activity.startActivityForResult(intent, CONTACTS_RESULT);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CONTACTS_RESULT && resultCode == RESULT_OK) {
String cell = "?", firstName = "?", lastName = "?";
uriContact = data.getData();
Cursor cursor = null;
mCell.setText(cell);
mLastName.setText(lastName);
mFirstName.setText(firstName);
}
}
}
Everything works fine up until this point. So my problem now is I would like to get the results when a user selects a contact. I tried below but the onActivityResult override method is never called. I've no idea what to do and seems that other people have had a different from mine and their solutions doesn't work for me.
Anyone know what I can do?
I'd recommend to use an interface for this. For example create the interface ContactCallback which you have to implement in your activity. When you create your adapter, pass this (which means the interface). When you click on an item call inside your adapter the passed callback like callback.onContactSelected(). Inside onContactSelected() which is implemented in your activity you can open your dialog.
Update:
To your problem that your onActivityResult() is never called:
Is there a line in your manifest called "android:launchMode="singleTask"? Remove it and it should be called. Following cases can cause an issue with receiving the result:
check you are using startActivityForResult() correctly, do not use startActivity().
if you do something in overriden onBackPressed method, super.onBackPressed(); has to be positioned at the last of the method, not at the first line.
remove android:launchMode="singleInstance" in manifest or equivalent argument to create a intent.
remove noHistory="true" in manifest of the called activity.
check setResult() is missed.
finish() is called to close the activity. use finishActivity() to close called activity.
use requestCode more than zero. negative value does not work.
You need to call this to get your onActivityResult called. on your dialog while your are closing put this code
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
How can I insert overridePendingTransition on a GridView `Adapter? In this way don't work, without transition startactivity work perfectly
bt.setOnLongClickListener(new OnLongClickListener(){
#Override
public boolean onLongClick(View v) {
final String selectedPad = Drum.pads[position];
Intent modPad = new Intent(v.getContext(), ModifyPad.class);
modPad.putExtra("pad", selectedPad);
context.startActivity(modPad);
overridePendingTransition(R.anim.exit_slid_in, R.anim.exit_slid_out);
return false;
}
});
I've read this post:
android start Activity in adapter (transition animiation direction problem), and comments related, but I don't know how pass the Activity in the Adapter. Any help?
Context is the Base Object of Activity ( see: What is the difference between Activity and Context? ),
so I used following:
Activity activity = (Activity) mContext;
activity.startActivity(repinIntent);
activity.overridePendingTransition(R.anim.act_start_in_from_right, R.anim.act_start_out_to_left);
Refers to: Getting activity from context in android
I am using the google maps api with overlay items and want to be able to get directions when the user clicks these overlays.
I want this linked to the middle button of my dialog:
// Middle button
dialog.setNeutralButton("Directions",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345"));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
}
});
I get the following error "The method startActivity(Intent) is undefined for the type new DialogInterface.OnClickListener(){}"
There are several answers to this on here but I cannot get any of them to work. I have tried both creating a constructor and calling getContext().
I am not sure if it is because my class is:
public class ItemizedOverlayLayoutActivity extends ItemizedOverlay {
Any help greatly appreciated.
Thanks
In the constructer of your class ItemizedOverlayLayoutActivity add a parameter Context:
private Context context;
public MyItemizedNewOverlay(Drawable defaultMarker, Context context) {
//...
this.context = context;
}
just before you create your dialog add:
final Context fcontext = context;
Then in the dialog middle button use:
fcontext.startActivity(intent);
good luck
I'm trying to get a class extending ItemizedOverlay to startActivity but there is a problem, it just won't compile.
I have a MapView that uses the ItemizedOverlay class to draw overlays but i want to start and activity when i tap on the screen.
Any idea how to solve this problem? Thanks
protected boolean onTap(int index) {
OverlayItem item = overlays.get(index);
String split_items = item.getSnippet();
Intent intent = new Intent();
intent.setClass(mainmenu,poiview.class);
startActivity(intent);
return true;
}
I had this problem so I have tested the example below.
The solution relies on calling "startActivity" from the MapActivity context.
If your map is actually working with overlays then you have already passed the MapView Context to your custom ItemizedOverlay constructor and you probably assigned the MapView Context to a class variable called mContext (I'm making assumptions that you followed Google's MapView example). So in your custom Overlay's onTap function, do this:
#Override
protected boolean onTap(int index) {
Intent intent = new Intent(mContext, ActivityYouAreTryingToLaunch.class);
mContext.startActivity(intent);
return true;
}
But you probably want to pass something to the new Activity you are trying to launch so your new activity can do something useful with your selection. So...
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
//assumption: you decided to store an "id" in the snippet so you can associate this map location with your new Activity
long id = Long.parseLong(item.getSnippet()); //Snippet is a built-in String property of an Overlay object.
//pass an "id" to the class so you can query
Intent intent = new Intent(mContext, ActivityYouAreTryingToLaunch.class);
String action = Intent.ACTION_PICK; //You can substitute with any action that is relevant to the class you are calling
//I create a URI this way because I append the id to the end of the URI (lookup the NotePad example for help because there are many ways to build a URI)
Uri uri = ContentUris.withAppendedId(Your_CONTENT_URI, id);
//set the action and data for this Intent
intent.setAction(action);
intent.setData(uri);
//call the class
mContext.startActivity(intent);
return true;
}
Try this one , i have done it with setting a positive button in the alert dialog...
protected boolean onTap(int index) {
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setIcon(R.drawable.info_icon);
dialog.setPositiveButton("Details", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent placeDetailsMapIntent = new Intent(mContext, PlaceDetailsActivity.class);
mContext.startActivity(placeDetailsMapIntent);
}
});
try to use following
Intent intent = new Intent(mainmenu.this, poview.class);
startActivity(intent);
I have an activity that has a button which opens a new MapActivity to select a location by tapping on the map.
The map has an overlay that overrides the onTap method to get the location but I want to return that location to the previous activity but, I don't know how to return the geopoint to the mapactivity in order to call the setResult() and finish() methods, because I can't call them from the Overlay.onTap method.
Any ideas?
Solved this way:
class tapOverlay extends Overlay
{
public GeoPoint lastTap=null;
String strCalle;
private Context context;
public tapOverlay(Context c)
{
this.context=c;
}
}
#Override
public boolean onTap(GeoPoint p, MapView mapView) {
lastTap = p;
mapView.getController().animateTo(p);
...
strCalle = sb.toString(); //from geocoder
...
devolverResultado();
return true;
}
private void devolverResultado()
{
MapActivity ma = (MapActivity) context;
Intent i = new Intent();
Bundle b = new Bundle();
b.putInt("dlat", lastTap.getLatitudeE6());
b.putInt("dlng", lastTap.getLongitudeE6());
b.putString("calle",strCalle);
i.putExtras(b);
ma.setResult(Activity.RESULT_OK, i);
ma.finish();
}
Call the new activity using an intent ...
Then , Use onActivityResult( int , int , Intent) to call the new activity from the current activity.....
U should get back the data from the new activity when u finish the called activity as the calling activity is placed on the stack ...
Hope this helps ... :)