Skip to content Skip to sidebar Skip to footer

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:

publicvoidsendToFavorites(Context context){
        String vID,vThumbnail,vTitle;
        vID = sendResult.getId().getVideoId();
        vThumbnail = sendResult.getSnippet().getThumbnails().getMedium().getUrl();
        vTitle = sendResult.getSnippet().getTitle();
        Intent fav = newIntent(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:

publicclassMyClassextends ... {

Intent fav;
//...publicvoidsendToFavorites(Context context){
        String vID,vThumbnail,vTitle;
        vID = sendResult.getId().getVideoId();
        vThumbnail = sendResult.getSnippet().getThumbnails().getMedium().getUrl();
        vTitle = sendResult.getSnippet().getTitle();
        fav = newIntent(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:

@OverridepublicbooleanonOptionsItemSelected(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 = newIntent(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);
    }
    returnsuper.onOptionsItemSelected(item);
}    

Post a Comment for "Getextras Not Working"