Why use the getbasecontext () method instead of this?

Source: Internet
Author: User

Q:ThisThe current context is often referenced. HoweverGetbasecontext ()To replaceThis. That is to say, useThisAn error is thrown.
Example:

Spinner spinner = (Spinner) findViewById(R.id.spinner);spinner.setAdapter(adapter);            spinner.setOnItemSelectedListener(new OnItemSelectedListener() {    @Override    public void onItemSelected(AdapterView<?>arg0, View arg1, int arg2, long arg3){       Toast.makeText(getBaseContext(),"SELECTED", Toast.LENGTH_SHORT).show(); //this line    }

WhenGetbasecontext ()ChangeThisThere will be errors.
Why must I useGetbasecontext ()Method, but cannot useThisWhat about it?

A:

1.Getapplicationcontext ()Method returns the context of the entire application lifecycle when the application is to be destroyed.
2.ThisThe application context returns the current context of the activity, which belongs to the activity. When it is destroyed, the activity is also destroyed. But in your case, it refers to the spinner instance, because weNitemselected (adapterview <?> Arg0, view arg1, int arg2, long arg3)Method. This method is from the spinner class, whileAdapterview. onitemselectedlistenerInterface to inherit this method.
3.Getbasecontext ()YesContextwrapper.

 

Reference: http://ask.csdn.net/questions/942

Why use the getbasecontext () method instead of this? (Transfer)

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.