From a5fe52a01fd1161474dbe6483fb7181817405357 Mon Sep 17 00:00:00 2001 From: Aayush Gupta Date: Thu, 19 Sep 2024 13:25:18 +0530 Subject: [PATCH] storage: demo: Switch to toolbar APIs for working with menu Activity methods have been deprecated Signed-off-by: Aayush Gupta --- .../grobox/storagebackuptester/LogFragment.kt | 22 ++++----- .../restore/DemoSnapshotFragment.kt | 2 +- .../scanner/MediaScanFragment.kt | 21 +++++---- .../settings/SettingsFragment.kt | 31 ++++++------ .../demo/src/main/res/layout/fragment_log.xml | 13 ++++- .../src/main/res/layout/fragment_scan.xml | 16 ++++++- .../demo/src/main/res/values-night/themes.xml | 2 +- storage/demo/src/main/res/values/themes.xml | 2 +- .../res/layout/fragment_backup_content.xml | 47 ++++++++++++------- 9 files changed, 98 insertions(+), 58 deletions(-) diff --git a/storage/demo/src/main/java/de/grobox/storagebackuptester/LogFragment.kt b/storage/demo/src/main/java/de/grobox/storagebackuptester/LogFragment.kt index 027ab534..38eb6f32 100644 --- a/storage/demo/src/main/java/de/grobox/storagebackuptester/LogFragment.kt +++ b/storage/demo/src/main/java/de/grobox/storagebackuptester/LogFragment.kt @@ -11,8 +11,6 @@ import android.os.Bundle import android.os.Environment import android.provider.Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION import android.view.LayoutInflater -import android.view.Menu -import android.view.MenuInflater import android.view.MenuItem import android.view.View import android.view.View.INVISIBLE @@ -22,6 +20,7 @@ import android.widget.Button import android.widget.ProgressBar import android.widget.Toast import android.widget.Toast.LENGTH_SHORT +import androidx.appcompat.widget.Toolbar import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.recyclerview.widget.RecyclerView @@ -48,7 +47,6 @@ open class LogFragment : Fragment() { container: ViewGroup?, savedInstanceState: Bundle?, ): View { - setHasOptionsMenu(true) val v = inflater.inflate(R.layout.fragment_log, container, false) list = v.findViewById(R.id.listView) list.adapter = adapter @@ -75,16 +73,16 @@ open class LogFragment : Fragment() { return v } - override fun onStart() { - super.onStart() - requireActivity().setTitle(R.string.app_name) + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + view.requireViewById(R.id.toolbar).apply { + title = getString(R.string.app_name) + setOnMenuItemClickListener(::onMenuItemSelected) + } } - override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { - inflater.inflate(R.menu.fragment_main, menu) - } - - override fun onOptionsItemSelected(item: MenuItem): Boolean { + private fun onMenuItemSelected(item: MenuItem): Boolean { return when (item.itemId) { R.id.settings -> { if (!checkPermission()) return false @@ -108,7 +106,7 @@ open class LogFragment : Fragment() { startActivity(shareIntent) true } - else -> super.onOptionsItemSelected(item) + else -> false } } diff --git a/storage/demo/src/main/java/de/grobox/storagebackuptester/restore/DemoSnapshotFragment.kt b/storage/demo/src/main/java/de/grobox/storagebackuptester/restore/DemoSnapshotFragment.kt index 13fcf328..dd5ab72b 100644 --- a/storage/demo/src/main/java/de/grobox/storagebackuptester/restore/DemoSnapshotFragment.kt +++ b/storage/demo/src/main/java/de/grobox/storagebackuptester/restore/DemoSnapshotFragment.kt @@ -31,7 +31,7 @@ class DemoSnapshotFragment : SnapshotFragment() { bottomStub.layoutResource = R.layout.footer_snapshot val footer = bottomStub.inflate() footer.findViewById