Rage
Rage

Reputation: 191

About android lifecycle

I read about the android activity's life cycle from here Dev guide. Now i have confusions that which part of code resides in which method, like onCreate, onStart, onResume, onRestart, onPause, onResume, onStop and onDestroy. Can you help me to put them in right place. Also the tracking should continue even when the application is minimized. I have the following code.

public class MainActivity extends FragmentActivity implements  LocationListener {

//List of user defined variables

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /*for now i  have kept everything in onCreate method
     * i have start and stop button to start the tracking and stop the tracking and show the distance of travel
    1. Checking whether GPS is on or OFF
    2. button = (ImageButton) findViewById(R.id.myButton);
    3. Code to load the Google map
    4. Now specified what start and stop button does.
        i. when i press start button it starts overlaying the path in the map and calculate distance travelled so far
        ii. when i press stop button it stops tracking and shows the details of final result in next activity.
    LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 2, this);
    */

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub
    //i hace code for tracking and overlaying here

}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}

}

Upvotes: 0

Views: 84

Answers (1)

RvdK
RvdK

Reputation: 19790

Based on the idea I think you want: "An app that has a start record and stop record button, meanwhile displays the route using google maps." Correct?

You will need 2 components:

Service (Background operations)

  • When started: listens for GPS updates and stores them internally.
  • When stopped: stop listening

Activity (GUI)

Has 2 buttons, start and stop recording, and Google Maps Widget.

  • On button start: use startService to start the Service
  • On button stop: use stopService to stop the Service
  • Location retrieving (between onResume and onPause): ask the Service (using binding or Messager) about the interally stored locations. When retrieved, show them with Google Maps widget.

Upvotes: 1

Related Questions