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

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

问题描述

我是 android 新手,我想了解 getApplication()getApplicationContext()、getBaseContext() 之间的区别>、getContext()someClass.this 尤其是在以下代码行中使用这些方法时:

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:

当我启动 Toast 时,这些有什么区别,我在哪些情况下使用它们?

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();

与意图相同:

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);

推荐答案

ToastIntent,都需要参考context.而 getApplicationgetApplicationContextLoginActivity.thisgetBaseContext,它们都提供对上下文的引用.

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

现在令人困惑的是不同上下文的声明及其特定用法.为简单起见,您应该计算 Android 框架中可用的两种类型的上下文.

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. 应用上下文
  2. 活动上下文

应用程序 上下文附加到应用程序的生命周期中,并且在应用程序的整个生命周期中始终相同.因此,如果您使用 Toast,您可以使用应用程序上下文甚至活动上下文(两者),因为 Toast 可以从应用程序中的任何位置引发,并且不附加到窗口.

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 上下文附加到 Activity 的生命周期,并且可以在引发 Activity 的 onDestroy() 时销毁.如果您想启动一个新活动,您必须在其 Intent 中使用活动的上下文,以便新启动的活动连接到当前活动(就活动堆栈而言).但是,您也可以使用应用程序的上下文来启动新活动,但是您需要设置标志 Intent.FLAG_ACTIVITY_NEW_TASK 以将其视为新任务.

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.

现在参考您的案例:

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

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() 虽然它指的是 Application 对象,但 Application 类扩展了 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() 提供应用程序上下文.

getApplicationContext() offers application context.

getBaseContext() 提供活动上下文.

提示:当你需要操作 Views 时,就去Activity-Context,否则 Application-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天全站免登陆