From 709eaf73e9120c5276e744284273f26630794633 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=8D=E3=82=80=E3=81=AD=E3=81=93?= Date: Tue, 19 Dec 2023 12:21:17 +0900 Subject: [PATCH] =?UTF-8?q?QRFragment=E3=81=A7=E3=83=9C=E3=82=BF=E3=83=B3?= =?UTF-8?q?=E3=82=92=E6=8A=BC=E3=81=97=E3=81=9F=E9=9A=9B=E3=81=ABHome?= =?UTF-8?q?=E3=81=AE=E7=8A=B6=E6=85=8B=E3=82=92=E6=9B=B8=E3=81=8D=E6=8F=9B?= =?UTF-8?q?=E3=81=88=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/childguard/HomeFragment.java | 22 +++++++++++++++++++ .../com/example/childguard/QRFragment.java | 14 ++++++++++++ 2 files changed, 36 insertions(+) diff --git a/app/src/main/java/com/example/childguard/HomeFragment.java b/app/src/main/java/com/example/childguard/HomeFragment.java index 6637e1b..7a7d163 100644 --- a/app/src/main/java/com/example/childguard/HomeFragment.java +++ b/app/src/main/java/com/example/childguard/HomeFragment.java @@ -1,12 +1,15 @@ package com.example.childguard; +import android.content.SharedPreferences; import android.os.Bundle; import androidx.fragment.app.Fragment; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.TextView; /** * A simple {@link Fragment} subclass. @@ -61,4 +64,23 @@ public class HomeFragment extends Fragment { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_home, container, false); } + + @Override + public void onResume() { + super.onResume(); + Log.d("HomeFragment", "onResume: called"); + TextView situationTextView = getView().findViewById(R.id.situation); + updateInCarStatus(situationTextView); + } + + public void updateInCarStatus(TextView situationTextView) { + SharedPreferences sharedPreferences = getActivity().getSharedPreferences("default", 0); + + Log.d("HomeFragment", "updateInCarStatus: " + sharedPreferences.getBoolean("inCar", false)); + if (sharedPreferences.getBoolean("inCar", false)) { + situationTextView.setText("\n降車状態"); + } else { + situationTextView.setText("\n乗車状態"); + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/childguard/QRFragment.java b/app/src/main/java/com/example/childguard/QRFragment.java index 218b31b..7e26151 100644 --- a/app/src/main/java/com/example/childguard/QRFragment.java +++ b/app/src/main/java/com/example/childguard/QRFragment.java @@ -5,6 +5,7 @@ import android.os.Bundle; import androidx.fragment.app.Fragment; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -67,6 +68,19 @@ public class QRFragment extends Fragment { Button cameraButton = view.findViewById(R.id.camera); + // Init shared preferences + SharedPreferences sharedPreferences = getActivity().getSharedPreferences("default", 0); + SharedPreferences.Editor editor = sharedPreferences.edit(); + + cameraButton.setOnClickListener(v -> { + Log.d("QRFragment", "onClick: called"); + if (!sharedPreferences.getBoolean("inCar", false)) { + editor.putBoolean("inCar", true); + } else { + editor.putBoolean("inCar", false); + } + editor.apply(); + }); return view; }