Pass value from one Android activity to another

1
0
-1
Since two activities have no shared space (by default), it is not possible to grab that value/variable from the original activity. One has to not only find a way to store this value in the original activity, but find a way to access it from the newly created activity. This allows data sharing between activities to some extent. The value has to accessible at activity initialization as well as at any other point in activity life-cycle.
  • irrelevant
  • passing knowledge
  • knowledgeable
  • expert
Total votes: 0
  • no complexity
  • simple
  • moderate
  • complex
  • very complex
Total votes: 1
Complexity
8 views.
Answer
1 answer

Use putExtra(...) function to pass values between Android activities

1
0
-1

Intent class function putExtra(String name, char[] value) is able to add extended data to the intent. The first parameter (name) must include a package prefix. For example if we wanted to create a new email activity and pass the value some email address we would do the following (assume we call from activity class):
Intent email = new Intent(getActivity(), EmailActivity.class);
email.putExtra(MainActivity.CURRENT_EMAIL, "someemail@domain.com");

where as MainActivity.CURRENT_EMAIL acts as a key in this case. Its value must contain package name, for example: "com.example.EMAIL". Further reading in documentation under section Extras: https://developer.android.com/guide/components/intents-filters.html

Taggings:

  • no complexity
  • simple
  • moderate
  • complex
  • very complex
Total votes: 1
Complexity