From d127c5401adae5b4588417b033ac81488fe8811f Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Wed, 24 May 2023 13:00:10 +0200 Subject: [PATCH] fix new onBackPressed handling this commit fixes bugs from d3056ff2825e53040ca49b86b84f91f73a3eb11c and 1185dcf17a99ca62efd32f8a53c298ec4fd6c4d0 fix onBackPressed handling to use OnBackPressedCallback remove unnecessary onBackPressedCallback's when they only finished the activity replaced some finishAffinity methods with finish ... Signed-off-by: Marcel Hibbe Signed-off-by: Andy Scherzinger --- .../talk/activities/CallBaseActivity.java | 21 +++++++---- .../activities/FullScreenImageActivity.kt | 8 ---- .../activities/FullScreenMediaActivity.kt | 8 ---- .../FullScreenTextViewerActivity.kt | 8 ---- .../nextcloud/talk/activities/MainActivity.kt | 21 +++++------ .../talk/activities/TakePhotoActivity.java | 37 ++++++++++--------- .../com/nextcloud/talk/chat/ChatActivity.kt | 19 ++++++---- .../talk/contacts/ContactsActivity.kt | 9 +---- .../ConversationInfoActivity.kt | 9 +---- .../ConversationInfoEditActivity.kt | 9 +---- .../ConversationsListActivity.kt | 16 ++++---- .../talk/location/GeocodingActivity.kt | 9 +---- .../talk/location/LocationPickerActivity.kt | 17 ++++++--- .../messagesearch/MessageSearchActivity.kt | 19 ++++++---- .../nextcloud/talk/profile/ProfileActivity.kt | 9 +---- .../activities/RemoteFileBrowserActivity.kt | 23 +++++------- .../talk/settings/SettingsActivity.kt | 9 +---- .../activities/SharedItemsActivity.kt | 8 ---- .../talk/translate/ui/TranslateActivity.kt | 2 +- 19 files changed, 102 insertions(+), 159 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/activities/CallBaseActivity.java b/app/src/main/java/com/nextcloud/talk/activities/CallBaseActivity.java index ece059e6e..261a3c058 100644 --- a/app/src/main/java/com/nextcloud/talk/activities/CallBaseActivity.java +++ b/app/src/main/java/com/nextcloud/talk/activities/CallBaseActivity.java @@ -35,6 +35,8 @@ import android.view.WindowManager; import com.nextcloud.talk.BuildConfig; +import androidx.activity.OnBackPressedCallback; + public abstract class CallBaseActivity extends BaseActivity { public static final String TAG = "CallBaseActivity"; @@ -43,6 +45,16 @@ public abstract class CallBaseActivity extends BaseActivity { public Boolean isInPipMode = Boolean.FALSE; long onCreateTime; + + private OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(true) { + @Override + public void handleOnBackPressed() { + if (isPipModePossible()) { + enterPipMode(); + } + } + }; + @SuppressLint("ClickableViewAccessibility") @Override public void onCreate(Bundle savedInstanceState) { @@ -58,6 +70,8 @@ public abstract class CallBaseActivity extends BaseActivity { if (isGreaterEqualOreo() && isPipModePossible()) { mPictureInPictureParamsBuilder = new PictureInPictureParams.Builder(); } + + getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback); } void hideNavigationIfNoPipAvailable(){ @@ -101,13 +115,6 @@ public abstract class CallBaseActivity extends BaseActivity { } } - @Override - public void onBackPressed() { - if (isPipModePossible()) { - enterPipMode(); - } - } - @Override protected void onUserLeaveHint() { long onUserLeaveHintTime = System.currentTimeMillis(); diff --git a/app/src/main/java/com/nextcloud/talk/activities/FullScreenImageActivity.kt b/app/src/main/java/com/nextcloud/talk/activities/FullScreenImageActivity.kt index bfd7c59c4..ddd5c0813 100644 --- a/app/src/main/java/com/nextcloud/talk/activities/FullScreenImageActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/activities/FullScreenImageActivity.kt @@ -33,7 +33,6 @@ import android.view.Menu import android.view.MenuItem import android.view.View import android.widget.Toast -import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.AppCompatActivity import androidx.core.content.FileProvider import com.nextcloud.talk.BuildConfig @@ -120,13 +119,6 @@ class FullScreenImageActivity : AppCompatActivity() { binding.photoView.visibility = View.VISIBLE displayImage(path) } - - val callback = object : OnBackPressedCallback(true) { - override fun handleOnBackPressed() { - finish() - } - } - onBackPressedDispatcher.addCallback(this, callback) } private fun displayImage(path: String) { diff --git a/app/src/main/java/com/nextcloud/talk/activities/FullScreenMediaActivity.kt b/app/src/main/java/com/nextcloud/talk/activities/FullScreenMediaActivity.kt index 4b44471ed..7a665183d 100644 --- a/app/src/main/java/com/nextcloud/talk/activities/FullScreenMediaActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/activities/FullScreenMediaActivity.kt @@ -30,7 +30,6 @@ import android.view.Menu import android.view.MenuItem import android.view.View import android.view.WindowManager -import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.AppCompatActivity import androidx.core.content.FileProvider import autodagger.AutoInjector @@ -115,13 +114,6 @@ class FullScreenMediaActivity : AppCompatActivity(), Player.Listener { } } ) - - val callback = object : OnBackPressedCallback(true) { - override fun handleOnBackPressed() { - finish() - } - } - onBackPressedDispatcher.addCallback(this, callback) } override fun onStart() { diff --git a/app/src/main/java/com/nextcloud/talk/activities/FullScreenTextViewerActivity.kt b/app/src/main/java/com/nextcloud/talk/activities/FullScreenTextViewerActivity.kt index dc057cd3e..00d17b7d5 100644 --- a/app/src/main/java/com/nextcloud/talk/activities/FullScreenTextViewerActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/activities/FullScreenTextViewerActivity.kt @@ -28,7 +28,6 @@ import android.content.Intent import android.os.Bundle import android.view.Menu import android.view.MenuItem -import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.AppCompatActivity import androidx.core.content.FileProvider import androidx.core.content.res.ResourcesCompat @@ -117,13 +116,6 @@ class FullScreenTextViewerActivity : AppCompatActivity() { ResourcesCompat.getColor(resources, R.color.bg_default, null) ) } - - val callback = object : OnBackPressedCallback(true) { - override fun handleOnBackPressed() { - finish() - } - } - onBackPressedDispatcher.addCallback(this, callback) } private fun readFile(fileName: String) = File(fileName).inputStream().readBytes().toString(Charsets.UTF_8) diff --git a/app/src/main/java/com/nextcloud/talk/activities/MainActivity.kt b/app/src/main/java/com/nextcloud/talk/activities/MainActivity.kt index c4174ae0a..78f4d3535 100644 --- a/app/src/main/java/com/nextcloud/talk/activities/MainActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/activities/MainActivity.kt @@ -85,6 +85,14 @@ class MainActivity : BaseActivity(), ActionBarProvider { private var router: Router? = null + private val onBackPressedCallback = object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + if (!router!!.handleBack()) { + finish() + } + } + } + @Suppress("Detekt.TooGenericExceptionCaught") override fun onCreate(savedInstanceState: Bundle?) { Log.d(TAG, "onCreate: Activity: " + System.identityHashCode(this).toString()) @@ -140,12 +148,7 @@ class MainActivity : BaseActivity(), ActionBarProvider { }) } - val callback = object : OnBackPressedCallback(true) { - override fun handleOnBackPressed() { - finish() - } - } - onBackPressedDispatcher.addCallback(this, callback) + onBackPressedDispatcher.addCallback(this, onBackPressedCallback) } fun lockScreenIfConditionsApply() { @@ -363,12 +366,6 @@ class MainActivity : BaseActivity(), ActionBarProvider { } } - fun handleOnBackPressed() { - if (!router!!.handleBack()) { - onBackPressedDispatcher.onBackPressed() - } - } - private fun logRouterBackStack(router: Router) { if (BuildConfig.DEBUG) { val backstack = router.backstack diff --git a/app/src/main/java/com/nextcloud/talk/activities/TakePhotoActivity.java b/app/src/main/java/com/nextcloud/talk/activities/TakePhotoActivity.java index 6a6f9c7a2..e7743ca2a 100644 --- a/app/src/main/java/com/nextcloud/talk/activities/TakePhotoActivity.java +++ b/app/src/main/java/com/nextcloud/talk/activities/TakePhotoActivity.java @@ -95,6 +95,23 @@ public class TakePhotoActivity extends AppCompatActivity { @Inject ViewThemeUtils viewThemeUtils; + private OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(true) { + @Override + public void handleOnBackPressed() { + Uri uri = (Uri) binding.photoPreview.getTag(); + + if (uri != null) { + File photoFile = new File(uri.getPath()); + if (!photoFile.delete()) { + Log.w(TAG, "Error deleting temp camera image"); + } + binding.photoPreview.setTag(null); + } + + finish(); + } + }; + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -221,26 +238,10 @@ public class TakePhotoActivity extends AppCompatActivity { finish(); } }, ContextCompat.getMainExecutor(this)); + + getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback); } - private OnBackPressedCallback callback = new OnBackPressedCallback(true) { - @Override - public void handleOnBackPressed() { - Uri uri = (Uri) binding.photoPreview.getTag(); - - if (uri != null) { - File photoFile = new File(uri.getPath()); - if (!photoFile.delete()) { - Log.w(TAG, "Error deleting temp camera image"); - } - binding.photoPreview.setTag(null); - } - - finish(); - } - }; - - private void showCameraElements() { binding.send.setVisibility(View.GONE); binding.retake.setVisibility(View.GONE); diff --git a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt index e86c9b90e..5f7c67410 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt @@ -74,6 +74,7 @@ import android.widget.ImageView import android.widget.PopupMenu import android.widget.RelativeLayout import android.widget.Toast +import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.AlertDialog import androidx.appcompat.view.ContextThemeWrapper import androidx.core.content.ContextCompat @@ -309,6 +310,14 @@ class ChatActivity : private var videoURI: Uri? = null + private val onBackPressedCallback = object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + val intent = Intent(this@ChatActivity, ConversationsListActivity::class.java) + intent.putExtras(Bundle()) + startActivity(intent) + } + } + var typingTimer: CountDownTimer? = null val typingParticipants = HashMap() @@ -362,6 +371,8 @@ class ChatActivity : initAdapter() binding.messagesListView.setAdapter(adapter) + + onBackPressedDispatcher.addCallback(this, onBackPressedCallback) } override fun onNewIntent(intent: Intent) { @@ -614,7 +625,7 @@ class ChatActivity : private fun setupActionBar() { setSupportActionBar(binding.chatToolbar) binding.chatToolbar.setNavigationOnClickListener { - handleOnBackPressed() + onBackPressedDispatcher.onBackPressed() } supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayShowHomeEnabled(true) @@ -623,12 +634,6 @@ class ChatActivity : viewThemeUtils.material.themeToolbar(binding.chatToolbar) } - fun handleOnBackPressed() { - val intent = Intent(this, ConversationsListActivity::class.java) - intent.putExtras(Bundle()) - startActivity(intent) - } - private fun initAdapter() { val senderId = if (!conversationUser!!.userId.equals("?")) { "users/" + conversationUser!!.userId diff --git a/app/src/main/java/com/nextcloud/talk/contacts/ContactsActivity.kt b/app/src/main/java/com/nextcloud/talk/contacts/ContactsActivity.kt index 916d355ee..fea14592b 100644 --- a/app/src/main/java/com/nextcloud/talk/contacts/ContactsActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/contacts/ContactsActivity.kt @@ -36,7 +36,6 @@ import android.view.Menu import android.view.MenuItem import android.view.View import android.view.inputmethod.EditorInfo -import androidx.activity.OnBackPressedCallback import androidx.appcompat.widget.SearchView import androidx.core.content.res.ResourcesCompat import androidx.core.view.MenuItemCompat @@ -93,12 +92,6 @@ class ContactsActivity : FlexibleAdapter.OnItemClickListener { private lateinit var binding: ControllerContactsRvBinding - private val callback = object : OnBackPressedCallback(true) { - override fun handleOnBackPressed() { - finish() - } - } - @Inject lateinit var userManager: UserManager @@ -195,7 +188,7 @@ class ContactsActivity : private fun setupActionBar() { setSupportActionBar(binding.contactsToolbar) binding.contactsToolbar.setNavigationOnClickListener { - onBackPressedDispatcher.addCallback(this, callback) + onBackPressedDispatcher.onBackPressed() } supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayShowHomeEnabled(true) diff --git a/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt b/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt index 58a8fdb5e..fdfd44db4 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt @@ -41,7 +41,6 @@ import android.view.View import android.view.View.GONE import android.view.View.VISIBLE import android.widget.Toast -import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.AlertDialog import androidx.appcompat.widget.SwitchCompat import androidx.work.Data @@ -110,12 +109,6 @@ class ConversationInfoActivity : private lateinit var binding: ActivityConversationInfoBinding - private val callback = object : OnBackPressedCallback(true) { - override fun handleOnBackPressed() { - finish() - } - } - @Inject lateinit var ncApi: NcApi @@ -202,7 +195,7 @@ class ConversationInfoActivity : private fun setupActionBar() { setSupportActionBar(binding.conversationInfoToolbar) binding.conversationInfoToolbar.setNavigationOnClickListener { - onBackPressedDispatcher.addCallback(this, callback) + onBackPressedDispatcher.onBackPressed() } supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayShowHomeEnabled(true) diff --git a/app/src/main/java/com/nextcloud/talk/conversationinfoedit/ConversationInfoEditActivity.kt b/app/src/main/java/com/nextcloud/talk/conversationinfoedit/ConversationInfoEditActivity.kt index 980274d3d..6eecacb88 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationinfoedit/ConversationInfoEditActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationinfoedit/ConversationInfoEditActivity.kt @@ -32,7 +32,6 @@ import android.view.Menu import android.view.MenuItem import android.view.View import android.widget.Toast -import androidx.activity.OnBackPressedCallback import androidx.core.net.toFile import androidx.core.view.ViewCompat import autodagger.AutoInjector @@ -72,12 +71,6 @@ class ConversationInfoEditActivity : private lateinit var binding: ActivityConversationInfoEditBinding - private val callback = object : OnBackPressedCallback(true) { - override fun handleOnBackPressed() { - finish() - } - } - @Inject lateinit var ncApi: NcApi @@ -158,7 +151,7 @@ class ConversationInfoEditActivity : private fun setupActionBar() { setSupportActionBar(binding.conversationInfoEditToolbar) binding.conversationInfoEditToolbar.setNavigationOnClickListener { - onBackPressedDispatcher.addCallback(this, callback) + onBackPressedDispatcher.onBackPressed() } supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayShowHomeEnabled(true) diff --git a/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt b/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt index e9590f96d..489a99ec4 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt @@ -189,7 +189,12 @@ class ConversationsListActivity : private var searchHelper: MessageSearchHelper? = null private var searchViewDisposable: Disposable? = null - private lateinit var callback: OnBackPressedCallback + private val onBackPressedCallback = object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + // TODO: replace this when conductor is removed. For now it avoids to load the MainActiviy which has no UI. + finishAffinity() + } + } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -204,12 +209,7 @@ class ConversationsListActivity : forwardMessage = intent.getBooleanExtra(KEY_FORWARD_MSG_FLAG, false) - callback = object : OnBackPressedCallback(true) { - override fun handleOnBackPressed() { - finish() - } - } - onBackPressedDispatcher.addCallback(this, callback) + onBackPressedDispatcher.addCallback(this, onBackPressedCallback) } override fun onResume() { @@ -256,7 +256,7 @@ class ConversationsListActivity : private fun setupActionBar() { setSupportActionBar(binding.conversationListToolbar) binding.conversationListToolbar.setNavigationOnClickListener { - onBackPressedDispatcher.addCallback(this, callback) + onBackPressedDispatcher.onBackPressed() } supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayShowHomeEnabled(true) diff --git a/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt b/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt index c14a63c6a..535356699 100644 --- a/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt @@ -33,7 +33,6 @@ import android.view.MenuItem import android.view.inputmethod.EditorInfo import android.widget.AdapterView import android.widget.Toast -import androidx.activity.OnBackPressedCallback import androidx.appcompat.widget.SearchView import androidx.core.view.MenuItemCompat import androidx.preference.PreferenceManager @@ -79,12 +78,6 @@ class GeocodingActivity : lateinit var adapter: GeocodingAdapter private var geocodingResults: List
= ArrayList() - private val callback = object : OnBackPressedCallback(true) { - override fun handleOnBackPressed() { - finish() - } - } - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) @@ -130,7 +123,7 @@ class GeocodingActivity : private fun setupActionBar() { setSupportActionBar(binding.geocodingToolbar) binding.geocodingToolbar.setNavigationOnClickListener { - onBackPressedDispatcher.addCallback(this, callback) + onBackPressedDispatcher.onBackPressed() } supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayShowHomeEnabled(true) diff --git a/app/src/main/java/com/nextcloud/talk/location/LocationPickerActivity.kt b/app/src/main/java/com/nextcloud/talk/location/LocationPickerActivity.kt index fc478d29c..fef8a16ab 100644 --- a/app/src/main/java/com/nextcloud/talk/location/LocationPickerActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/location/LocationPickerActivity.kt @@ -41,6 +41,7 @@ import android.view.MenuItem import android.view.View import android.view.inputmethod.EditorInfo import android.widget.Toast +import androidx.activity.OnBackPressedCallback import androidx.appcompat.widget.SearchView import androidx.core.content.PermissionChecker import androidx.core.content.res.ResourcesCompat @@ -117,6 +118,13 @@ class LocationPickerActivity : var searchItem: MenuItem? = null var searchView: SearchView? = null + private val onBackPressedCallback = object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + setResult(Activity.RESULT_CANCELED) + finish() + } + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) @@ -137,6 +145,8 @@ class LocationPickerActivity : setupSystemColors() getInstance().load(context, PreferenceManager.getDefaultSharedPreferences(context)) + + onBackPressedDispatcher.addCallback(this, onBackPressedCallback) } override fun onStart() { @@ -177,7 +187,7 @@ class LocationPickerActivity : private fun setupActionBar() { setSupportActionBar(binding.locationPickerToolbar) binding.locationPickerToolbar.setNavigationOnClickListener { - handleOnBackPressed() + onBackPressedDispatcher.onBackPressed() } supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayShowHomeEnabled(true) @@ -574,11 +584,6 @@ class LocationPickerActivity : // empty } - fun handleOnBackPressed() { - setResult(Activity.RESULT_CANCELED) - finishAffinity() - } - companion object { private val TAG = LocationPickerActivity::class.java.simpleName private const val REQUEST_PERMISSIONS_REQUEST_CODE = 1 diff --git a/app/src/main/java/com/nextcloud/talk/messagesearch/MessageSearchActivity.kt b/app/src/main/java/com/nextcloud/talk/messagesearch/MessageSearchActivity.kt index 1bf287b4f..a4f8373c6 100644 --- a/app/src/main/java/com/nextcloud/talk/messagesearch/MessageSearchActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/messagesearch/MessageSearchActivity.kt @@ -31,6 +31,7 @@ import android.view.Menu import android.view.MenuItem import android.view.View import android.widget.Toast +import androidx.activity.OnBackPressedCallback import androidx.appcompat.widget.SearchView import androidx.lifecycle.ViewModelProvider import autodagger.AutoInjector @@ -74,6 +75,13 @@ class MessageSearchActivity : BaseActivity() { private var searchViewDisposable: Disposable? = null private var adapter: FlexibleAdapter>? = null + private val onBackPressedCallback = object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + setResult(Activity.RESULT_CANCELED) + finish() + } + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) @@ -92,6 +100,8 @@ class MessageSearchActivity : BaseActivity() { binding.swipeRefreshLayout.setOnRefreshListener { viewModel.refresh(searchView.query?.toString()) } + + onBackPressedDispatcher.addCallback(this, onBackPressedCallback) } private fun setupActionBar() { @@ -212,7 +222,7 @@ class MessageSearchActivity : BaseActivity() { } override fun onMenuItemActionCollapse(item: MenuItem): Boolean { - handleOnBackPressed() + onBackPressedDispatcher.onBackPressed() return false } }) @@ -238,15 +248,10 @@ class MessageSearchActivity : BaseActivity() { .subscribe { newText -> viewModel.onQueryTextChange(newText) } } - fun handleOnBackPressed() { - setResult(Activity.RESULT_CANCELED) - finishAffinity() - } - override fun onOptionsItemSelected(item: MenuItem): Boolean { return when (item.itemId) { android.R.id.home -> { - handleOnBackPressed() + onBackPressedDispatcher.onBackPressed() true } else -> super.onOptionsItemSelected(item) diff --git a/app/src/main/java/com/nextcloud/talk/profile/ProfileActivity.kt b/app/src/main/java/com/nextcloud/talk/profile/ProfileActivity.kt index 0a8e005d0..136b364ee 100644 --- a/app/src/main/java/com/nextcloud/talk/profile/ProfileActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/profile/ProfileActivity.kt @@ -41,7 +41,6 @@ import android.view.MenuItem import android.view.View import android.view.ViewGroup import android.widget.Toast -import androidx.activity.OnBackPressedCallback import androidx.annotation.DrawableRes import androidx.core.content.ContextCompat import androidx.core.net.toFile @@ -89,12 +88,6 @@ import javax.inject.Inject class ProfileActivity : BaseActivity() { private lateinit var binding: ActivityProfileBinding - private val callback = object : OnBackPressedCallback(true) { - override fun handleOnBackPressed() { - finish() - } - } - @Inject lateinit var ncApi: NcApi @@ -194,7 +187,7 @@ class ProfileActivity : BaseActivity() { private fun setupActionBar() { setSupportActionBar(binding.profileToolbar) binding.profileToolbar.setNavigationOnClickListener { - onBackPressedDispatcher.addCallback(this, callback) + onBackPressedDispatcher.onBackPressed() } supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayShowHomeEnabled(true) diff --git a/app/src/main/java/com/nextcloud/talk/remotefilebrowser/activities/RemoteFileBrowserActivity.kt b/app/src/main/java/com/nextcloud/talk/remotefilebrowser/activities/RemoteFileBrowserActivity.kt index c684f3779..f1647ffc4 100644 --- a/app/src/main/java/com/nextcloud/talk/remotefilebrowser/activities/RemoteFileBrowserActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/remotefilebrowser/activities/RemoteFileBrowserActivity.kt @@ -76,6 +76,13 @@ class RemoteFileBrowserActivity : AppCompatActivity(), SelectionInterface, Swipe private var filesSelectionDoneMenuItem: MenuItem? = null + private val onBackPressedCallback = object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + setResult(Activity.RESULT_CANCELED) + finish() + } + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) @@ -113,6 +120,8 @@ class RemoteFileBrowserActivity : AppCompatActivity(), SelectionInterface, Swipe binding.sortButton.setOnClickListener { changeSorting() } viewModel.loadItems() + + onBackPressedDispatcher.addCallback(this, onBackPressedCallback) } private fun initViewModel(mimeTypeSelectionFilter: String?) { @@ -194,18 +203,6 @@ class RemoteFileBrowserActivity : AppCompatActivity(), SelectionInterface, Swipe return true } - fun handleOnBackPressed() { - val callback = object : OnBackPressedCallback(true) { - override fun handleOnBackPressed() { - setResult(Activity.RESULT_CANCELED) - finish() - } - } - - onBackPressedDispatcher.addCallback(this, callback) - callback.handleOnBackPressed() - } - override fun onResume() { super.onResume() refreshCurrentPath() @@ -223,7 +220,7 @@ class RemoteFileBrowserActivity : AppCompatActivity(), SelectionInterface, Swipe override fun onOptionsItemSelected(item: MenuItem): Boolean { return when (item.itemId) { android.R.id.home -> { - handleOnBackPressed() + onBackPressedDispatcher.onBackPressed() true } R.id.files_selection_done -> { diff --git a/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt b/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt index 8c3db9dfd..55fd82bbf 100644 --- a/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt @@ -54,7 +54,6 @@ import android.widget.Checkable import android.widget.EditText import android.widget.LinearLayout import android.widget.Toast -import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.AlertDialog import androidx.core.content.ContextCompat import androidx.core.view.ViewCompat @@ -110,12 +109,6 @@ import javax.inject.Inject class SettingsActivity : BaseActivity() { private lateinit var binding: ActivitySettingsBinding - private val callback = object : OnBackPressedCallback(true) { - override fun handleOnBackPressed() { - finish() - } - } - @Inject lateinit var ncApi: NcApi @@ -255,7 +248,7 @@ class SettingsActivity : BaseActivity() { private fun setupActionBar() { setSupportActionBar(binding.settingsToolbar) binding.settingsToolbar.setNavigationOnClickListener { - onBackPressedDispatcher.addCallback(this, callback) + onBackPressedDispatcher.onBackPressed() } supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayShowHomeEnabled(true) diff --git a/app/src/main/java/com/nextcloud/talk/shareditems/activities/SharedItemsActivity.kt b/app/src/main/java/com/nextcloud/talk/shareditems/activities/SharedItemsActivity.kt index 4f47024bc..d06a5f983 100644 --- a/app/src/main/java/com/nextcloud/talk/shareditems/activities/SharedItemsActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/shareditems/activities/SharedItemsActivity.kt @@ -30,7 +30,6 @@ import android.os.Bundle import android.util.Log import android.view.MenuItem import android.view.View -import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.AppCompatActivity import androidx.core.content.res.ResourcesCompat import androidx.lifecycle.ViewModelProvider @@ -106,13 +105,6 @@ class SharedItemsActivity : AppCompatActivity() { }) viewModel.initialize(user, roomToken) - - val callback = object : OnBackPressedCallback(true) { - override fun handleOnBackPressed() { - finish() - } - } - onBackPressedDispatcher.addCallback(this, callback) } private fun handleModelChange( diff --git a/app/src/main/java/com/nextcloud/talk/translate/ui/TranslateActivity.kt b/app/src/main/java/com/nextcloud/talk/translate/ui/TranslateActivity.kt index 7a5aef723..48ee313de 100644 --- a/app/src/main/java/com/nextcloud/talk/translate/ui/TranslateActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/translate/ui/TranslateActivity.kt @@ -127,7 +127,7 @@ class TranslateActivity : BaseActivity() { private fun setupActionBar() { setSupportActionBar(binding.translationToolbar) binding.translationToolbar.setNavigationOnClickListener { - onBackPressed() + onBackPressedDispatcher.onBackPressed() } supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayShowHomeEnabled(true)