Use toast "Go" outside activity/fragment

Source: Internet
Author: User

Use Toast in Activity

This is the most basic use of toast, the first parameter is the context, generally in the activity we use this instead, representing the caller's instance as activity.

 Public classTestdbactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. OnCreate (savedinstancestate);                Setcontentview (R.LAYOUT.TESTDB2); Toast.maketext ( This, "Welcome to Concise modern Magic ~", Toast.length_short). Show (); //Toast.maketext (Getapplicationcontext (), "Welcome to Concise modern Magic ~", Toast.length_short). Show ();    }}

It is also possible to replace this with Getapplicationcontext ().

and to a button of the onclick (view view) and other methods, we use this when the error, So we might use activityname.this to solve the problem, mainly because the class that implements the context has several models specific to Android, Activity, service, and Broadcastreceiver.

Use Toast in Fragment

In Fragment, you need to use getactivity () to get the Context object, as follows:

 Public class extends Fragment {        @Override    publicvoid  onCreate (Bundle savedinstancestate) {         Super. OnCreate (savedinstancestate);                " Welcome to Concise Modern Magic ~ ", Toast.length_short). Show ();    })
Use Toast in non-activity/fragment

If you want to use a Toast in a class other than activity/fragment, you still use This/getapplicationcontext (), you will generally report such a mistake:

The method Getapplicationcontext () is undefined

Because the context is not available, how do you get to it in this class? You can pass the Context over.

When instantiating a class, the Context is passed as a parameter:

New MyClass (this);

Then write a constructor that receives the Context as a parameter.

 Public class MyClass {    Context C;      Public MyClass (Context context)    {         = context;     }}

You can then use the Toast:

 Public void onproviderdisabled (String provider) {    "Gps Disabled", Toast.length_short);}

Turn from: Institute of Modern Magic

Use toast "Go" outside activity/fragment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.