LoginSignup
3
5

More than 5 years have passed since last update.

【Android】ChromeCustomTabsを閉じたときに任意の処理を実行する

Posted at

概要

ChromeCustomTabsを閉じてアプリに戻ってきたときに何かしらの処理を実行したいときのやり方メモです。

簡易まとめ

  1. CustomTabsIntent#launchUrl()実行前にActivityのインスタンス変数にフラグを立てる
  2. ChromeCustomTabsを閉じるとActivity#onResume()に戻ってくるのでフラグが立っていたら処理実行してフラグを下げる

コード例

フラグを定義

MainActivity.java
// ChromeCustomTabs用フラグ
private boolean flag = false;

launchUrl()するところでフラグを立てる

MainActivity.java
@Override
public void onClick(View v) {
    // launchUrl()前にフラグを立てておく
    flag = true;

    String url = "https://example.com/";
    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    CustomTabsIntent customTabsIntent = builder.build();
    customTabsIntent.launchUrl(this, Uri.parse(url));
}

ChromeCustomTabsを閉じるとActivity#onResume()に戻ってくるのでフラグが立っていたら処理実行してフラグを下げる

MainActivity.java
@Override
protected void onResume() {
    super.onResume();

    // CustomTabsを閉じるとActivityのonResume()に戻ってくるのでフラグで判定
    if (flag) {
        // やりたい処理を実行
        doSomething();

        // フラグを下げておく
        flag = false;
    }
}

参考

3
5
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
3
5