QRFragmentでボタンを押した際にHomeの状態を書き換えるように
This commit is contained in:
parent
c946f29fab
commit
8c6660d34a
|
@ -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乗車状態");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user