Is it possible to move the app in background when we click on device back button in android?
background means application moves to screen(application) from where it launches ?
Is it possible to move the app in background when we click on device back button in android?
background means application moves to screen(application) from where it launches ?
Try Using
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
moveTaskToBack(true);
return true;
}
return false;
}
@Override
public void onBackPressed() {
moveTaskToBack(true);
}
Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
onBackPressedDispatcher.addCallback(this) { moveTaskToBack(true) }
}
I will prefer to use below code to move application in background
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(isTaskRoot()) {
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(homeIntent);
return true;
}
else {
super.keyDown(keyCode,event);
return false;
}
default:
super.keyDown(keyCode,event);
return false;
}
}