android - How to display a Toast using a Fragment within a Navigation Drawer -
i'm trying have toast displayed after select menu item navigation drawer , app switches fragment. have line of code inside oncreate() method fragment displayed when fragment inflated, except it's not working:
public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { myview = inflater.inflate(r.layout.post_layout, container, false); //my toast wont work!!:( toast.maketext(post_fragment.this, "it worked!", toast.length_short).show(); return myview; } any thoughts? help.
try changing:
toast.maketext(post_fragment.this, "it worked!", toast.length_short).show(); to
toast.maketext(getactivity(), "it worked!", toast.length_short).show(); or put maketext() call inside onnavigationitemselected() method in activity houses fragment. in case call similar this.
toast.maketext(this, "it workded!", toast.length_short).show(); the first parameter maketext() context object. docs context is:
interface global information application environment. abstract class implementation provided android system. allows access application-specific resources , classes, up-calls application-level operations such launching activities, broadcasting , receiving intents, etc.
your activity class extends context. fragment class not. method you're calling static, not have access application-specific resources , classes/etc. passing context (your activity), gives access resources.
cheers
Comments
Post a Comment