Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8238193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:43:38+00:00 2026-06-07T19:43:38+00:00

In my app, I am using two location managers for two different activities. First

  • 0

In my app, I am using two location managers for two different activities. First location manager is created when the first activity is created. And then from the first activity I am creating a second activity which is creating a second location manager. In the second activity, I am trying to stop location manager updates using the following code when the back button is pressed:

@Override
public void onBackPressed() {
  lm.removeUpdates(ll);
  ll = null;
  lm = null;
  finish();
}

ll and lm are declared globally in both first and second activity seperatly.
ll and lm are initialized in onCreate of second activity using following code:

 lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
 ll = new myLocationListener();
 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, ll);

The problem is that if I don’t go to the second activity and stop the first activity’s location updates, then the GPS sign in my Android goes away. But if I go to the second activity, come back to the first activity using the back button and then stop the first acivity’s location updates, then the GPS location sign is still there. I am stopping updates in the first activity using same line of codes:

lm.removeUpdates(ll);
ll = null;
lm = null;

ll and lm in first activity are initialized when start button is pressed:

 public void startClick (View target){
      lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
      ll = new myLocationListener();
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, ll);
 }

Is there any kind of mistake in the code or is there any logical error? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:43:40+00:00Added an answer on June 7, 2026 at 7:43 pm

    You can have a single instance of the LocationManager and store it in your Application Context. To do so you need to create a class that extends Application. Here’s an example:

    public class MyApplication extends Application 
    {
       private LocationManager locationManager;
    
       @Override
       public void onCreate()
       {
         super.onCreate();
    
         //Initialize here the locationManager or initialize it in one of your
         //Activities. It is your choice.
       }
    
       //To retrieve the locationManager
       public LocationManager getLocationManger()
       {
          return locationManager;
       }
    
       //In case you want to initialize the locationManger in one of your Activities
       public void setLocationManager(LocationManager locationMangerIn) 
       {
           locationManager = locationMangerIn;
       }
    }
    

    Do not forget to add a reference to MyApplication in the android:name attribute in the AndroidManifest.xml file. Something like this:

    <application android:icon="@drawable/icon" android:label="@string/app_name"
            android:name="com.your_package_name.MyApplication">
    

    To access your locationManager instance you call this piece of code:

    LocationManger lm = ((MyApplication)activitysContext.getApplication()).getLocationManger();
    

    To instantiate the locationManager instance inside your Activity you use this:

    public void startClick (View target){
      LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    
      //You can add the listener now or later. Again, your choice.
      ll = new myLocationListener();
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, ll);
    
      ((MyApplication)activitysContext.getApplication()).setLocationManger(lm);
    

    }

    Hope that helps:)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using core location framework inside my app.The didUpdateToLocation method is called two
I would like to build two different versions of my app using different android:minSdkVersion.
I am creating an app, in which I have two small imageview (using have
I have a Weather app with four Activities. The main/launcher activity is 'invisible' using...
I am working on a reporting app using PivotViewer. There are two controls on
When Using UIPickerController I have two choices on my App: take a picture take
I'm using core data in my app. I have two entities that are related:
I am creating an app using iOS 5 SDK. I managed to push views
I wanted to run (using Cassini) two copies of my web application from the
I'm creating a little calendar app in Django. I have two model classes; Calendar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.