
Contents
指定した時間後に処理を実行する方法
Androidにて指定した時間後に処理を実行する方法です。
この方法でアプリのタイトル画面を実装することも可能です。
Handler().postDelayed
○秒後に処理を実行したいときは Handler.postDelayed(); を使うことで実装できます。
Handler handler = new Handler(getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
IntroductionFragment introductionFragment = new IntroductionFragment();
FragmentTransaction introductionTransaction = getSupportFragmentManager().beginTransaction();
introductionTransaction.addToBackStack(null);
introductionTransaction.replace(R.id.fragment_container, introductionFragment);
introductionTransaction.commit();
}
}, 3000);
こちらの例では、3,000ミリ秒・つまり3秒後に IntroductionFragment の生成が行われます。
今回はFragment生成というUI更新に関わる処理のため、
必ずメインスレッドで処理を行いたかったので、
Handler()の引数に getMainLooper() を渡しています。
スレッドにこだわりがない場合であれば、引数は指定する必要がありません。
⬇️のような実装方法でもOK
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
/* 3,000ms後に行いたい処理 */
}
}, 3000);
Handlerってなに?
HandlerはAndroid内で処理が行われているスレッドを扱う際に使うものです。
スレッドとはアプリの処理の実行単位です。
今回のように、処理を遅延させるケース、別スレッドに実行処理を移すケースで使います。
厳密に理解していなくても、こういう時に使えるんだな程度の理解でやっていけます。使用しているうちに理解してきます。

Handler().postDelayedを使ってタイトル画面を実装してみる
アプリ起動時にタイトル画面を表示して、
3秒後にタイトル画面を削除してアプリのメイン画面に遷移する実装を行ってみます。
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
/* Fragmentを表示するためにActivity画面の準備*/
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* タイトル画面の表示 */
TitleFragment titleFragment = new TitleFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, titleFragment);
transaction.commit();
}
@Override
protected void onResume() {
super.onResume();
/* タイトル画面が表示された3秒後にアプリメイン画面を表示 */
Handler handler = new Handler(getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
IntroductionFragment introductionFragment = new IntroductionFragment();
FragmentTransaction introductionTransaction = getSupportFragmentManager().beginTransaction();
introductionTransaction.addToBackStack(null);
introductionTransaction.replace(R.id.fragment_container, introductionFragment);
introductionTransaction.commit();
}
}, 3000);
}
}
onCreate()内でタイトル画面となるTitleFragmentを立ち上げます。
onResume()内でpostDelayedを使い、3秒後にアプリのメイン画面となるIntroductionFragmentを立ち上げます。
onCreate()内でタイトル画面を立ち上げるのは、
Home画面や別画面に移動してから戻ってきた際に、一回一回タイトル画面を表示することを防ぐためです。

ソースコード全容
最後に上記で貼り付けたGIFサンプルアプリのソースコードを貼り付けておきます。
今回の肝となっているのはMainActivityで、TitleFragmentとIntroductionFragmentは画面を表示しているだけです。
参考になれば幸いです!
ご精読ありがとうございました!!
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
tools:ignore="MissingConstraints">
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
title_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:fontFamily="@font/rajdhani_medium"
android:text="TITLE SPLASH"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#101BE1"
android:textSize="160px"
android:textStyle="bold" />
/>
</FrameLayout>
introduction_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/introduction_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:fontFamily="@font/rajdhani_medium"
android:text="SAMPLE"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#101BE1"
android:textSize="90px"
android:textStyle="bold"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/introduction_ok_button"
android:layout_width="200dp"
android:layout_height="75dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="750dp"
android:backgroundTint="#1AADC7"
android:fontFamily="@font/convergence"
android:text="@android:string/ok"
android:textColor="#FFFFFF"
android:textSize="90px" />
</FrameLayout>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
/* Fragmentを表示するためにActivity画面の準備*/
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* タイトル画面の表示 */
TitleFragment titleFragment = new TitleFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, titleFragment);
transaction.commit();
}
@Override
protected void onResume() {
super.onResume();
/* タイトル画面が表示された3秒後にアプリメイン画面を表示 */
Handler handler = new Handler(getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
IntroductionFragment introductionFragment = new IntroductionFragment();
FragmentTransaction introductionTransaction = getSupportFragmentManager().beginTransaction();
introductionTransaction.addToBackStack(null);
introductionTransaction.replace(R.id.fragment_container, introductionFragment);
introductionTransaction.commit();
}
}, 3000);
}
}
TitleFragment.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class TitleFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.title_fragment, container, false);
return view;
}
}
IntroductionFragment.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class IntroductionFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.introduction_fragment, container, false);
return view;
}
}