差以及何时使用getApplication(),getApplicationContext(),getBaseContext()和someClass.this [英] difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this

查看:192
本文介绍了差以及何时使用getApplication(),getApplicationContext(),getBaseContext()和someClass.this的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid和我试图理解之间的的区别getApplication() getApplicationContext(), getBaseContext()的getContext() someClass.this 尤其是当使用这些方法在下面的code线:

当我启动举杯什么是它们之间以及在何种情况下的差异,我使用它们?

  Toast.makeText(LoginActivity.this登录成功,Toast.LENGTH_SHORT).show();
Toast.makeText(getApplication()登录成功,Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext()登录成功,Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext()登录成功,Toast.LENGTH_SHORT).show();
 

同意向:

 意向意图=新的意图(getApplicationContext(),LoginActivity.class);
意向意图=新的意图(MenuPag​​ina,LoginActivity.class。);
意向意图=新的意图(getBaseContext(),LoginActivity.class);
意向意图=新的意图(getApplication(),LoginActivity.class);
 

解决方案

吐司意图,既需要提及的上下文的。和 getApplication getApplicationContext LoginActivity.this getBaseContext ,它们都提供了参考范围内。

现在的事情混淆了不同的背景和他们的特定用途的声明。为了让事情简单,你应该算两种类型的上下文可以在Android框架。

  1. 应用程序上下文
  2. 活动上下文

应用程序上下文连接到应用程序的生命周期,并且总是在整个应用程序生命周期相同。所以,如果你正在使用的吐司的,你可以使用应用程序上下文,甚至活动上下文(两者),因为敬酒可以从任何地方提出,在您的应用程序,而不是附加到一个窗口。

活动上下文连接到活动的生命周期,如果活动的的onDestroy()上升可以被摧毁。如果你想推出一个新的活动,你必须要使用活动的情况下在其的意图的,以使新的发射活动连接到当前的活动(在活动栈方面)。但是,你可以使用应用程序的情况下也推出了新的活动,但你需要设置标志 Intent.FLAG_ACTIVITY_NEW_TASK 的意图把它作为一个新的任务。

现参照你的情况:

LoginActivity.this 虽然它指的是你自己的类,扩展了活动类,但基类(活动)也扩展Context类,因此它可以被用来提供活动上下文。

getApplication()虽然它指的是应用程序对象,但应用程序类扩展了Context类,因此它可以用来提供应用程序上下文。

getApplicationContext()提供应用程序上下文。

getBaseContext()提供活动场景。

  

温馨提示:当你需要操作浏览然后去   活动 - 上下文的,否则的应用上下文的就足够了。

I'm new to android and I'm trying to understand the difference between getApplication(), getApplicationContext(), getBaseContext(), getContext() and someClass.this and especially when to use the these methods in the following code lines:

When I launch a toast what is the difference between these and in which cases to I use them?

Toast.makeText(LoginActivity.this, "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplication(), "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(), "LogIn successful", Toast.LENGTH_SHORT).show();

same with intents:

Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
Intent intent = new Intent(MenuPagina., LoginActivity.class);
Intent intent = new Intent(getBaseContext(), LoginActivity.class);
Intent intent = new Intent(getApplication(), LoginActivity.class);

解决方案

Toast and Intent, both requires reference to context. And getApplication, getApplicationContext, LoginActivity.this and getBaseContext, they all offer reference to the context.

Now the thing confuses is the declaration of different contexts and their specific-usage. To make things simple, you should count two types of context available in the Android framework.

  1. Application Context
  2. Activity Context

Application context is attached to the application's life-cycle and will always be same throughout the life of application. So if you are using Toast, you can use application context or even activity context (both) because a toast can be raised from anywhere with in your application and is not attached to a window.

Activity context is attached to the Activity's life-cycle and can be destroyed if the activity's onDestroy() is raised. If you want to launch a new activity, you must need to use activity's context in its Intent so that the new launching activity is connected to the current activity (in terms of activity stack). However, you may use application's context too to launch a new activity but then you need to set flag Intent.FLAG_ACTIVITY_NEW_TASK in intent to treat it as a new task.

Now referring to your cases:

LoginActivity.this though its referring to your own class which extends Activity class but the base class (Activity) also extends Context class, so it can be used to offer activity context.

getApplication() though its referring to Application object but the Application class extends Context class, so it can be used to offer application context.

getApplicationContext() offers application context.

getBaseContext() offers activity context.

Tips: Whenever you need to manipulate Views then go for Activity-Context, else Application-Context would be enough.

这篇关于差以及何时使用getApplication(),getApplicationContext(),getBaseContext()和someClass.this的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆