Android: passing parameters between classes

I have class2 that class1 is involved in creating clicks. I need to pass some parameters / objects from class1 to class2 . I know only the standard way, which does not have the ability to pass parameters.

// launch the full article Intent i = new Intent(this, Class2.class); startActivity(i); 
+7
android android-intent parameters
source share
1 answer

You can use Intent.putExtra (which uses the Bundle ) to transfer additional data.

 Intent i = new Intent(this, Class2.class); i.putExtra("foo", 5.0f); i.putExtra("bar", "baz"); startActivity(i); 

Then, when you enter the new Activity :

 Bundle extras = getIntent().getExtras(); if(extras !=null) { float foo = extras.getFloat("foo"); String bar = extras.getString("bar"); } 

This allows you to transfer basic data to the "Activity". However, you may need a little more work to pass arbitrary objects.

+14
source share

All Articles