`

【Android】this和this.getApplictionContext的区别 (转载)

阅读更多

 

在android中常常会遇到与context有关的内容,特写下这篇blog,浅论一下context : 在语句 AlertDialog.Builder builder = new AlertDialog.Builder(this); 中,要求传递的 参数就是一个context,在这里我们传入的是this,那么这个this究竟指的是什么东东呢? 这里的this指的是Activity.this,是这个语句所在的Activity的this,是这个Activity 的上下文。网上有很多朋友在这里传入this.getApplicationContext(),这是不对的。 AlertDialog对象是依赖于一个View的,而View是和一个Activity对应的。 于是,这里涉及到一个生命周期的问题,this.getApplicationContext()取的是这个应 用程序的Context,Activity.this取的是这个Activity的Context,这两者的生命周期是不同 的,前者的生命周期是整个应用,后者的生命周期只是它所在的Activity。而AlertDialog应 该是属于一个Activity的,在Activity销毁的时候它也就销毁了,不会再存在;但是,如果传 入this.getApplicationContext(),就表示它的生命周期是整个应用程序,这显然超过了它 的生命周期了。 所以,在这里我们只能使用Activity的this。

 

差别就是代表着不同的生命周期吧。

分享到:
评论

相关推荐

    Android.Studio.Development.Essentials.Android.6.Edition.1519722087

    Fully updated for Android 6, the goal of this book is to teach the skills necessary to develop Android based applications using the Android Studio Integrated Development Environment (IDE) and the ...

    Learn.Android.Studio.Build.Android.Apps.Quickly.and.Effectively

    Learn Android Studio covers Android Studio and its rich tools ecosystem, including Git and Gradle: this book covers how Android Studio works seamlessly with Git, for source control, and Gradle, a ...

    Android代码-Auth0.Android

    Auth0.Android Android Java toolkit ...Those using this library from version 1.14.0 and up should start targeting latest android SDK versions, as recommended by Google. Those running into conflicts b

    Android悬浮窗菜单FloatMenuSample.zip

     .addMenuItem(android.R.color.transparent, R.drawable.yw_menu_account, Const.MENU_ITEMS[0], android.R.color.black, this)  .addMenuItem(android.R.color.transparent, R.drawable.yw_menu_favour, ...

    Android.SQLite.Essentials.1783282959

    This is an ideal book for Android programmers who want to explore SQLite databases based on Android applications. The general competency level expected of the reader is prior knowledge of developing ...

    android 省市二级联动数据

    ArrayAdapter<CharSequence> aAdapter = ArrayAdapter.createFromResource(this, R.array.province, android.R.layout.simple_spinner_item); aAdapter.setDropDownViewResource(android.R.layout.simple_spinner_...

    android-support-multidex.jar

    使用android-support-multidex解决Dex超出方法数的限制问题,让你的应用不再爆棚.Google在API 21中提供了通用的解决方案,那就是android-support-multidex.jar. 这个jar包最低可以支持到API 4的版本(Android L及以上...

    Android.Programming.The.Big.Nerd.Ranch.Guide

    Based on Big Nerd Ranch's popular Android Bootcamp course, this guide will lead you through the wilderness using hands-on example apps combined with clear explanations of key concepts and APIs. This ...

    MT6589_Android_scatter_emmc.txt

    MT6589_Android_scatter_emmc.txt 配置文件

    Android之泡泡效果bubble.rar

    Android之泡泡效果bubble package com.ray.bubble; import android.app.Activity; import android.content.Context; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import ...

    Apress.Android.Recipes.2nd.Edition.2012

    This book is a handy reference for all Android app developers. Newcomers may also find this of interest, too. Table of Contents Chapter 1. Preliminary Information: Before We Get Started Chapter 2. ...

    Mastering.Android.Wear.Application.Development

    This book is for application developers (web, mobile, and desktop) who are interested in building new wearable apps, and mobile developers who already have apps on iTunes or Google Play Store and are ...

    Pro.Android.Wearables.Building.Apps.for.Smartwatches.1430265507

    Pro Android Wearables details how to design and build Android Wear apps for new and unique Android wearable device types, such as Google Android smartwatches, which use the new WatchFaces API, as well...

    Android.Studio.Game.Development.Concepts.and.Design.14842171

    This one of a kind short book walks any Android developer through the process of creating mobile games using the new Android Studio IDE. Android Studio offers a myriad of tools for developers such as ...

    Android support.v7包

    Android support.v7包

    Android.Quick.APIs.Reference

    This book is a quick, handy syntax reference for experienced Android programmers, and a concise, easily-digested introduction for other programmers new to Android. Table of Contents Chapter 1. ...

    Android this与Activity.this的区别

    写语句的时候有两种情况: ... 用英文在google搜what‘s difference between this and Activity.this,终于有了结果(其实自己后面用了Java里ClassName.this和this 之前搜的是Activity.this,所以没有结果,这

    android-support-v4.jar已打包进去源代码

    android-support-v4.jar默认不能为它指定源代码,查看或调试时很不方便,我重新打了下包,把源代码打了进去,可以直接查看和调试。

    Android.6.Essentials.1785884417

    This book is for Android developers who are looking to move their applications into the next Android version with ease. What You Will Learn Familiarize yourself with the features of Android 6 Code ...

    Learning.Android.Application.Testing.1784395331

    If you are an Android developer looking to test your applications or optimize your application development process, then this book is for you. No previous experience in application testing is required...

Global site tag (gtag.js) - Google Analytics