Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

概要

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

参考

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした