In this video it shows the steps to use the onBackPressed override method to customize the functionality of back button of the Android phone. In this method one can navigate to the desired layout and page when back button is pressed using setContentView API.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: [ Ссылка ] or email at: programmerworld1990@gmail.com
Complete source code and other details/ steps of this video are posted in the below link:
[ Ссылка ]
However, the main Java code is copied below also for reference:
package com.programmerworld.backpressedlayoutapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private int intLayout = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonGoToSecondLayout(View view){
intLayout = 2;
setContentView(R.layout.second_layout);
}
public void buttonGoToMainLayout(View view){
intLayout = 1;
setContentView(R.layout.activity_main);
}
@Override
public void onBackPressed() {
if (intLayout == 2){
intLayout = 1;
setContentView(R.layout.activity_main);
}
else {
super.onBackPressed();
}
}
}
-
![](https://s2.save4k.ru/pic/O1UqQhhA3Ns/maxresdefault.jpg)