Skip to content Skip to sidebar Skip to footer

How To Put Extras To Different Activity?

For example, i have 3 activities.. I also use Tab host with separate activity when switch tab widget. 1) FoodTypeListActivity.class:there is a list of food type i.e. Breakfast, Din

Solution 1:

You should just use shared preference.

In your listview click use this to put the values in shared preference.

SharedPreferencesitems= getSharedPreferences("my_food_data", 0);
  SharedPreferences.Editoreditor= settings.edit();
  editor.putInt("foodTypeID", 3);
  editor.putString("foodTypeName", "Breakfast"); 
  editor.commit(); //VERY important

Now to pull the data out in ANY activity use..

SharedPreferencesitems= getSharedPreferences("my_food_data", 0);
   intfoodId= settings.getInt("foodTypeId", 0);
   StringfoodTitle= items.getString("foodTypeName", "No Name"); //the default value of of "no name will be used if nothing is in the preference file.

I think this works well for what you are trying to do.

Solution 2:

That looks exactly correct. Now on your receiving activity just use the following code to retrieve your value.

int foodTypeId = getIntent().getIntExtra("foodTypeID", -1);
    String foodTypeName= getIntent().getStringExtra("foodTypeName");

The -1 is the default value if there is not value with the given key. That default value should never actually be needed unless you forget to pass in a value using the given key.

Post a Comment for "How To Put Extras To Different Activity?"