16
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Toastの終了を検出する

Last updated at Posted at 2013-05-16

Toastが終了してから何かしたい、ってことがあるとします。Toastにはイベントリスナは付けられないのでToastが持つviewの onDetachedWindow()をオーバーライドして代用できます。

final Toast toast = Toast.makeText(context, "hoge", Toast.LENGTH_SHORT);
new FrameLayout(this) {
	{
		addView(toast.getView()); // toastのviewをframelayoutでくるむ
		toast.setView(this); // framelayoutを新しくtoastに設定する
	}
	@Override
	public void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		// Toastが終了したあとの処理をする
	}
};
toast.show();

検索用キーワード: Toast, onDismiss

検証用コード: https://github.com/gfx/android-ToastOnDismiss/blob/master/ToastOnDismiss/src/main/java/com/example/toastondismiss/MainActivity.java

16
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?