i want to make a back navigation logic using this code :
<code>backBtn.setOnClickListener(v -> {
onBackPressed();
});
otherUsername.setText(otherUser.getUsername());
</code>
<code>backBtn.setOnClickListener(v -> {
onBackPressed();
});
otherUsername.setText(otherUser.getUsername());
</code>
backBtn.setOnClickListener(v -> {
onBackPressed();
});
otherUsername.setText(otherUser.getUsername());
but it seem that the onBackPressed is deprecated now, so when i run the app and click the back button it worked for a bit but then it goes to the previous page as if i was double clicking it, and when i try to do it the second time the app will crash.
How to solve this ?
for context this is my full code :
<code>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());
}
}
</code>
<code>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());
}
}
</code>
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());
}
}
1