android xml context,Android Context

Context的概述

Context提供对应用程序状态信息的访问。它提供Activities、Fragments、和 Services去访问resource files、images、themes/styles和外部目录地址。它还允许访问Android的内置服务,例如layout inflation、keyboard和content providers。

在许多情况下,当需要Context时,我们只需要传递当前activity的实例即可。当我们位于在activity创建的内部对象中,例如adapters或fragments,我们需要将activity实例传递到这些对象中。当我们不在activity(在application或service中)的情况下,我们可以使用Application Context替代Context。

Context的用途

显示启动组件

// Provide context if MyActivity is an internal activity.

Intent intent = new Intent(context, MyActivity.class);

startActivity(intent);

当显示打开组件时,需要以下两个参数:

包名称:标识应用程序包含的组件

组件的完全限定的Java类名称

如果要启动一个内部组件,则可以传递Context,因为当前应用程序的包名称可以通过context.getPackageName()获取。

创建一个View

TextView textView = new TextView(context);

Context包含下列views所需的信息:

设备屏幕大小和尺寸信息,用于将dp、sp转换为px

styled attributes

onClick属性的activity引用

创建一个XML布局文件

我们使用Context来获取LayoutInflater,以便将XML布局infalte到内存中

// A context is required when creating views.

LayoutInflater inflater = LayoutInflater.from(context);

inflater.inflate(R.layout.my_layout, parent);

发送一个本地广播

当发送或注册广播的接收器时,我们使用Context来获取LocalBroadcastManager:

// The context contains a reference to the main Looper,

// which manages the queue for the application's main thread.

Intent broadcastIntent = new Intent("custom-action");

LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

检索系统服务

从应用程序发送通知,需要NotificationManager系统服务:

// Context objects are able to fetch or start system services.

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

int notificationId = 1;

// Context is required to construct RemoteViews

Notification.Builder builder = new Notification.Builder(context).setContentTitle("custom title");

notificationManager.notify(notificationId, builder.build());

Application Context与Activity Context

虽然主题和样式通常使用Application Context,不过它们也能使用Activity Context定义。Activity可以具有与Application的其他部分不同的一组主题或样式。(例如,如果需要为某些页面禁用ActionBar)你会注意到在AndroidManifest.xml文件中通常在application标签中添加一个android:theme属性。我们还可以为activity标签指定不同的一个。

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name=".MainActivity"

android:label="@string/app_name"

android:theme="@style/MyCustomTheme">

因此,了解Application Context和Activity Context各自的生命周期的持续时间很重要。大多数Views应该传递一个Activity Context,以获得应该使用什么主题、样式、尺寸的信息。如果没有为activity显示指定主题,则默认使用application指定的主题。

在大多数情况下,你应该使用Activity上下文。通常,当在Activity内部使用Context时,可以使用Java中的关键字this引用类的实例,下面的示例显示了如何使用此方法显示Toast消息:

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();

}

}

匿名函数中使用Context

我们在实现监听器时使用匿名函数,Java中的关键字this应用于声明最近的类。在这些情况下,必须指定外部类MainActivity来引用Activity实例。

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

TextView tvTest = (TextView) findViewById(R.id.abc);

tvTest.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_SHORT).show();

}

});

}

}

Adapters中Context的使用

Array Adapter

当为ListView构建适配器时,通常在布局inflation时使用getContext()。通常使用此方法构造ArrayAdapter:

if (convertView == null) {

convertView =

LayoutInflater

.from(getContext())

.inflate(R.layout.item_user, parent, false); }

如果你使用Application Context实例化ArrayAdapter,则需注意到主题/样式没有应用。因此,请确保你在这些情况下传递了Activity Context。

RecyclerView Adapter

public class MyRecyclerAdapter extends RecyclerView.Adapter {

@Override

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View v = LayoutInflater .from(parent.getContext()) .inflate(itemLayout, parent, false);

return new ViewHolder(v);

}

@Override

public void onBindViewHolder(ViewHolder viewHolder, int i) {

// If a context is needed, it can be retrieved

// from the ViewHolder's root view.

Context context = viewHolder.itemView.getContext();

// Dynamically add a view using the context provided.

if(i == 0) {

TextView tvMessage = new TextView(context);

tvMessage.setText("Only displayed for the first item.") ;

viewHolder.customViewGroup.addView(tvMessage);

}

}

public static class ViewHolder extends RecyclerView.ViewHolder {

public FrameLayout customViewGroup;

public ViewHolder(view imageView) {

// Very important to call the parent constructor

// as this ensures that the imageView field is populated.

super(imageView);

// Perform other view lookups.

customViewGroup = (FrameLayout) imageView.findById(R.id.customViewGroup);

}

}

}

不管何时,ArrayAdapter都需要一个Context来传递给它的构造函数,而RecyclerView.Adapter不需要,当需要inflation时,可以从父视图得到正确的Context。

如果在onCreateViewHolder()方法外部需要Context,只要有一个ViewHolder实例即可,就可以通过viewHolder.itemView.getContext()来获取Context。itemView是ViewHolder基类中的public、non-null、final字段。

----段

避免内存泄露

Application Context通常需要在创建单例时使用,例如在多个Activities中都需要通过Context来获取系统服务的自定义管理器类。当不再运行后,由于保留对Activity Context的引用将导致内存不回收,因此需要使用Application Context替代。

在下面的例子中,如果所存储的Context是一个Activity或者Service,当它们被系统销毁,因为CustomManager类包含一个静态引用,则不能够进行垃圾回收。

pubic class CustomManager {

private static CustomManager sInstance;

public static CustomManager getInstance(Context context) {

if (sInstance == null) {

// This class will hold a reference to the context

// until it's unloaded. The context could be an Activity or Service.

sInstance = new CustomManager(context);

}

return sInstance;

}

private Context mContext;

private CustomManager(Context context) {

mContext = context;

}

}

正确存储Context

为了避免内存泄露,不要持有超过Context生命周期的引用。检查你的后台线程、pending handlers或内部类等任何可能持有Context的对象。

正确的方法是将Application Context存储在CustomManager.getInstance()中。Application Context是一个单例,并与应用程序进程的生命周期相关联,所以可以安全地存储对它的引用。

当在组件的生命周期之外又或是独立于传递的Context的生命周期需要使用到Context时,则使用Application Context。

public static CustomManager getInstance(Context context) {

if (sInstance == null) {

// When storing a reference to a context, use the application context.

// Never store the context itself, which could be a component.

sInstance = new CustomManager(context.getApplicationContext());

}

return sInstance;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值