假设我在onCreate()
中有此代码
for (int i = 0; i < 20; i++) {
Toast.makeText(MainActivity.this, "Toast "+i, Toast.LENGTH_SHORT).show();
}
当我启动该应用程序时,吐司开始弹出。
现在,当我按下后退按钮时(假设Toast 5之后)。 onDestroy()
被调用,应用已关闭。
但是我仍然可以看到Toast弹出,直到达到20,或者我从内存中清除了应用程序。
问题:
为什么我的代码用完了应用程序?
我已经给出了活动的上下文,那么活动一旦销毁,它不应该立即停止吗?
context
在这里不重要吗?
如果链接任何文档,将很有帮助。
在Toast
类中,Toast.makeText()
是静态method
。当您调用此方法时,将创建一个新的Toast
对象,并将您传递的Context
保存在其中,并使用系统的默认布局创建一个view
,该对象会附加到Toast
对象上,并且还会设置引力管理toast
在屏幕上的显示位置。
您的toast
由系统服务显示。该服务维护要显示的queue
的toast messages
,并使用其自己的Thread
显示它们。当您在show()
上调用toast object
时,它将toast
放入系统服务的消息队列中。因此,当创建activity
后破坏20 toast
时,系统服务已经开始起作用,并且在message queue
中显示了消息。通过在activity
(销毁)系统上按回键,不能得出您可能不打算显示剩余吐司消息的结论。仅当您从内存中清除应用程序时,系统才能自信地推断它不再需要从应用程序中显示toast message
。
有关更多信息,您可以查看Toast class
的源代码。我正在为您提供相关方法。顺便说一句好问题👍🏻
Toast.makeText的实现
/**
* Make a standard toast to display using the specified looper.
* If looper is null, Looper.myLooper() is used.
* @hide
*/
public static Toast makeText(@NonNull Context context, @Nullable Looper looper,
@NonNull CharSequence text, @Duration int duration) {
Toast result = new Toast(context, looper);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
tv.setText(text);
result.mNextView = v;
result.mDuration = duration;
return result;
}
创建新吐司:
/**
* Constructs an empty Toast object. If looper is null, Looper.myLooper() is used.
* @hide
*/
public Toast(@NonNull Context context, @Nullable Looper looper) {
mContext = context; // your passed `context` is saved.
mTN = new TN(context.getPackageName(), looper);
mTN.mY = context.getResources().getDimensionPixelSize(
com.android.internal.R.dimen.toast_y_offset);
mTN.mGravity = context.getResources().getInteger(
com.android.internal.R.integer.config_toastDefaultGravity);
}
show()的实现
/**
* Show the view for the specified duration.
*/
public void show() {
if (mNextView == null) {
throw new RuntimeException("setView must have been called");
}
INotificationManager service = getService();
String pkg = mContext.getOpPackageName();
TN tn = mTN;
tn.mNextView = mNextView;
final int displayId = mContext.getDisplayId();
try {
service.enqueueToast(pkg, tn, mDuration, displayId);
} catch (RemoteException e) {
// Empty
}
}