-6

I am trying to pass parameters between two activities which are tabs of tabhost. How do I send a value correctly from an activity (Active.java) to a second activity (Active2.java)? What did I do wrong?


Active.java

public class Active extends Activity {

   private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_active);

      // intent = new Intent(this,Active2.class);
      intent = getParent().getIntent();   
    }
    public void Location(View view) {

    final LocationManager LocMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        final LocationListener LocList = new LocationListener() {

            @Override
            public void onLocationChanged(Location location) {


                double latitude = location.getLatitude();
                double longitude = location.getLongitude();

                LatLng point = new LatLng(latitude, longitude);
                intent.putExtra("point", point);
                intent.putExtra("Active",1);
                TabActivity ta = (TabActivity) Active.this.getParent();
                ta.getTabHost().setCurrentTab(1);
           }
    }; 
}

Active2.java

public class Active2 extends Activit {

  private GoogleMap googleMap;
  private GoogleApiClient client;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_active2);

      getFragmentManager().findFragmentById(R.id.map)).getMap();
      client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
      updateGoogleMap();
    }
   void updateGoogleMap ()
   {

     Bundle extras = getIntent().getExtras();
      if(null !=extras) {

          LatLng point = (LatLng) extras.get("point");
          TextView text = (TextView) findViewById(R.id.GpsStat) ;
          text.setText("bylem");

          googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
          googleMap.addMarker(new MarkerOptions().position(point).title("point"));
      }
  }
}
Draken
  • 3,049
  • 13
  • 32
  • 49
detector
  • 35
  • 8
  • 1
    where are you sending data? You need to put all values in Intent and then pass that intent in `startActivity(intent)` – SweetWisher ツ Jun 17 '16 at 11:21
  • try this http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android or http://stackoverflow.com/questions/19286970/using-intents-to-pass-data-between-activities-in-android – Abhishek Patel Jun 17 '16 at 11:21
  • 1
    Activities as tabs has been deprecated since API 13. You should be using `Fragment`s instead. – Mike M. Jun 17 '16 at 11:27
  • If you type this in google you get lot of answer. **How can I pass parameters between two activities?** link for google search - https://www.google.co.in/search?client=ubuntu&channel=fs&q=How+can+I+pass+parameters+between+two+activities%3F&ie=utf-8&oe=utf-8&gfe_rd=cr&ei=FOhjV_zsIJKnvwSB_5GwBA – Ranjith Kumar Jun 17 '16 at 12:07
  • `How do I pass... ` ... by using a Bundle. – Phantômaxx Jun 17 '16 at 12:09

1 Answers1

1

Firstly i want to say that you have lots of error in your code. yo should make more improvement in its quality. read more.

now answer to your question.

  • You have to start the activity in Active.java to send it to Active2.java activity.
  • You have to give create new intent in Active.java and this will help you to put all the intent values into the intent.
  • Then you can start the startActivity() and start the Active2.java.
  • Then only you will have those values in the Active2.java and you can retrive all those values here.

so it would be like this

in Active.java

    Intent intent = new intent(Active.this,Active2.class);
    intent.putExtra("point", point);
    intent.putExtra("Active",1);
    startActivity(intent);

in Active2.java

    Intent intent=getIntent();
    String point=intent.getStringExtra("point");
    int Active=intent.getIntExtra("Active");
Sagar Nayak
  • 1,997
  • 2
  • 17
  • 41