Nirav Kamani
Nirav Kamani

Reputation: 3272

Reloading map activity does not show the marker again

I am creating very simple application and trying to learn android.

In this application i created one activity which contains Map and it also has option menu on the click of option menu item opens up another activity and closes current map here is the code.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent = new Intent();

    if (item.getItemId() == R.id.LegalNotice) {
        intent.setClass(Map.this, LegalNotices.class);
    } else if (item.getItemId() == R.id.ChangeDistance) {
        intent.setClass(Map.this, ChangeDefaultDistance.class);
    }

    startActivity(intent);
    this.finish();

    return true;
}

Now here ChangeDefaultDistance is activity which contains only list view. Now when particular item is selected from list view i am again starting the Map activity.Here is the code.

@Override
        public void onItemClick(AdapterView<?> arg0, View view,
                int position, long id) {

            TextView tv;

            tv = (TextView) view;

            Map.distance = tv.getText().toString();

            startActivity(new Intent(ChangeDefaultDistance.this, Map.class));

        }

Now when again map starts it executes onCreate() method but previously in onCreate method i zoomed map to particular position and drawn marker now it is not zoomed and not drawing marker also.When i debugged the code i found that it is executing the code but nothing is happening.

Can anybody tell me why this is happening?

Here is the code of onCreate method also.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (isGooglePlayAvailable()) {

        criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);

        // Getting the service from context and giving to location_manager
        location_manager = (LocationManager) getSystemService(LOCATION_SERVICE);

        setContentView(R.layout.mapdemo);

        placesTask = new PlacesTask();

        getGoogleMap();

        getUserLocation();

        google_map.clear();

        Toast.makeText(Map.this, "Hello Sexy!!!", Toast.LENGTH_LONG).show();

        Toast.makeText(this, "Latitude:" + lat + " Longitude:" + lang,
                Toast.LENGTH_LONG).show();

        drawMarker();

        // If Distance is not choosed default distance is given
        if (distance.equals("")) {
            distance = "1000";
        }

        sb = createUrl();

        placesTask.execute(sb);

    }

}

All the functions are working properly.

But it is not zooming again and drawing marker what can be the problem?

Upvotes: 0

Views: 318

Answers (1)

Emil Adz
Emil Adz

Reputation: 41099

The reason you are not seeing the changes you applied before you opened the ListView Activity is that you are starting the Map Activity from scrach and it's going throw onCreate method again and basiclly create all the views in this Activity again.

in simple words look at this as if you are creating another instanse of the same Acitivty and now you will have two activities of the same type in your activities stack.

What you should do is instead of starting the list activity with the startActivity method, you should start it with startActivityForResult and return a result from your list Acitivty to your already created Map Activity instead of starting a new Activity all over again.

Take a look at this question to get an idea how to use the startActivityForResult method:

How to manage `startActivityForResult` on Android?

and at the docs:

http://developer.android.com/training/basics/intents/result.html

Upvotes: 1

Related Questions