Android: Pass information to a second Activity

It’s fairly easy to pass information in what Android called a Bundle. When you start an activity from the main activity, you can set to put some information in the bundle and pass it in the intent into the new activity.

For example, the following code starts a new activity class call mapview. Passing to mapview 3 double values. Given the key “LAT”, “LONG”, “ALT” etc. So each parameter you passed is a key associated pair of values.

Intent i = new Intent(getApplicationContext(), mapview.class);
		i.putExtra("LAT", dLat );
		i.putExtra("LONG", dLong);
		i.putExtra("ALT", dAlt);
		startActivityForResult(i, 0);



Then in the activity you try to pass information to, (the secondary activity), call the function getExtras().
In the following code, in the secondary activity that we just want to pass 3 double value to.
Call the getIntent() function will return an instance of current intent, then use that intent to getExtras() to grab the things we put in from the main activity using the function putExtra.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview);
 
        Bundle myBundle = getIntent().getExtras();
        dLat = myBundle.getDouble("LAT");
        dLong = myBundle.getDouble("LONG");
        dAlt = myBundle.getDouble("ALT");
 
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>