我想使用此代码制作后退导航逻辑:
backBtn.setOnClickListener(v -> {
onBackPressed();
});
otherUsername.setText(otherUser.getUsername());
但似乎 onBackPressed 现在已被弃用,所以当我运行应用程序并单击后退按钮时,它工作了一段时间,但随后它会转到上一页,就像我双击它一样,当我尝试这样做时第二次应用程序将崩溃。
如何解决这个问题?
对于上下文,这是我的完整代码:
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.activity.OnBackPressedCallback;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import com.daffakhairy.easychat.model.UserModel;
import com.daffakhairy.easychat.utils.AndroidUtil;
public class ChatActivity extends AppCompatActivity {
UserModel otherUser;
EditText messageInput;
ImageButton sendMessageBtn;
ImageButton backBtn;
TextView otherUsername;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
// Get UserModel
otherUser = AndroidUtil.getUserModelFromIntent(getIntent());
messageInput = findViewById(R.id.chat_message_input);
sendMessageBtn = findViewById(R.id.message_send_btn);
backBtn = findViewById(R.id.back_btn);
otherUsername = findViewById(R.id.other_username);
recyclerView = findViewById(R.id.chat_recycler_view);
backBtn.setOnClickListener(v -> {
onBackPressed();
});
otherUsername.setText(otherUser.getUsername());
}
}
从 Android API 级别 33 开始,onBackPressed() 方法已被弃用。现在推荐的方法是通过 OnBackPressedCallback 使用 OnBackPressedDispatcher。 这种方法允许您更干净地处理后退导航逻辑并避免意外行为。
解决方案:使用OnBackPressedDispatcher 您可以使用 OnBackPressedCallback 显式处理 Activity 中的后退导航。以下是您的代码的修改版本:
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.activity.OnBackPressedCallback;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import com.daffakhairy.easychat.model.UserModel;
import com.daffakhairy.easychat.utils.AndroidUtil;
public class ChatActivity extends AppCompatActivity {
UserModel otherUser;
EditText messageInput;
ImageButton sendMessageBtn;
ImageButton backBtn;
TextView otherUsername;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
// Get UserModel
otherUser = AndroidUtil.getUserModelFromIntent(getIntent());
messageInput = findViewById(R.id.chat_message_input);
sendMessageBtn = findViewById(R.id.message_send_btn);
backBtn = findViewById(R.id.back_btn);
otherUsername = findViewById(R.id.other_username);
recyclerView = findViewById(R.id.chat_recycler_view);
// Set the username
if (otherUser != null) {
otherUsername.setText(otherUser.getUsername());
} else {
otherUsername.setText("Unknown User");
}
// Handle back button click
backBtn.setOnClickListener(v -> finish()); // Directly finish the `activity
// Handle system back button using OnBackPressedDispatcher
OnBackPressedCallback callback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
// Custom back navigation logic
finish(); // End the activity
}
};
// Add the callback to the dispatcher
getOnBackPressedDispatcher().addCallback(this, callback);
}
}
使用 OnBackPressedCallback:
向OnBackPressedDispatcher注册以拦截后退按钮按下。