GetExtras Not Working
I'm trying to send Extras from Activity A to B. The sending part is good, and the variables are not nulls, but I can't figure out how can I get them in Activity B. This is the code
Solution 1:
public void sendToFavorites(Context context){
String vID,vThumbnail,vTitle;
vID = sendResult.getId().getVideoId();
vThumbnail = sendResult.getSnippet().getThumbnails().getMedium().getUrl();
vTitle = sendResult.getSnippet().getTitle();
Intent fav = new Intent(context,Favorites.class);
fav.putExtra("title",vTitle);
fav.putExtra("thumbnail",vThumbnail);
fav.putExtra("id",vID);
}
Your Intent object fav only applies to this method. As you never call startActivity(fav) in this method, then however you are starting Activity B will not include your bundled data. There are two possible alternatives - either declare fav as a class variable and reference it later, like so:
public class MyClass extends ... {
Intent fav;
//...
public void sendToFavorites(Context context){
String vID,vThumbnail,vTitle;
vID = sendResult.getId().getVideoId();
vThumbnail = sendResult.getSnippet().getThumbnails().getMedium().getUrl();
vTitle = sendResult.getSnippet().getTitle();
fav = new Intent(context,Favorites.class);
fav.putExtra("title",vTitle);
fav.putExtra("thumbnail",vThumbnail);
fav.putExtra("id",vID);
}
//...later, during a button click....
if (fav != null) {
startActivity(fav);
}
Or, as I would prefer, place the logic from your sendToFavourites() method inside your click event:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.favorites:
String vID,vThumbnail,vTitle;
vID = sendResult.getId().getVideoId();
vThumbnail = sendResult.getSnippet().getThumbnails().getMedium().getUrl();
vTitle = sendResult.getSnippet().getTitle();
Intent fav = new Intent(context, Favorites.class); //Could use "this" instead of context if this class extends Activity
fav.putExtra("title",vTitle);
fav.putExtra("thumbnail",vThumbnail);
fav.putExtra("id",vID);
startActivity(fav);
}
return super.onOptionsItemSelected(item);
}
Post a Comment for "GetExtras Not Working"