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 <dev@mhibbe.de>
Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Marcel Hibbe 2023-05-24 13:00:10 +02:00 committed by Andy Scherzinger
parent dff9ea5651
commit d127c5401a
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B
19 changed files with 102 additions and 159 deletions

View file

@ -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();

View file

@ -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) {

View file

@ -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() {

View file

@ -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)

View file

@ -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

View file

@ -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);

View file

@ -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<String, String>()
@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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<Address> = 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)

View file

@ -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

View file

@ -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<AbstractFlexibleItem<*>>? = 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)

View file

@ -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)

View file

@ -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 -> {

View file

@ -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)

View file

@ -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(

View file

@ -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)