The procedure in the question linked below does not appear to work for me either with the standard or the support ActionMode. ACTION_UP never appears as a parameter although ACTION_DOWN does but regardless, the ActionMode is still cancelled.
Prevent to cancel Action Mode by press back button
Can anybody suggest how to prevent the action mode from closing when the back key is pressed?
My code
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class test extends AppCompatActivity {
Toolbar toolbar;
Button standard;
Button support;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
toolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
standard= (Button) findViewById(R.id.standard);
standard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActionMode(standardCallback);
standard.setVisibility(View.GONE);
support.setVisibility(View.GONE);
}
});
support= (Button) findViewById(R.id.support);
support.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startSupportActionMode(supportCallback);
standard.setVisibility(View.GONE);
support.setVisibility(View.GONE);
}
});
}
private android.view.ActionMode.Callback standardCallback=new android.view.ActionMode.Callback() {
@Override
public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
mode.setTitle("Standard Action Mode Enabled");
return true;
}
@Override
public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
return false;
}
@Override
public void onDestroyActionMode(android.view.ActionMode mode) {
standard.setVisibility(View.VISIBLE);
support.setVisibility(View.VISIBLE);
}
};
//requires theme entry: <item name="windowActionModeOverlay">true</item>
private android.support.v7.view.ActionMode.Callback supportCallback=new android.support.v7.view.ActionMode.Callback() {
@Override
public boolean onCreateActionMode(android.support.v7.view.ActionMode mode, Menu menu) {
mode.setTitle("Support Action Mode Enabled");
return true;
}
@Override
public boolean onPrepareActionMode(android.support.v7.view.ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(android.support.v7.view.ActionMode mode, MenuItem item) {
return false;
}
@Override
public void onDestroyActionMode(android.support.v7.view.ActionMode mode) {
standard.setVisibility(View.VISIBLE);
support.setVisibility(View.VISIBLE);
}
};
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
// handle your back button code here
Toast.makeText(this,"Back ACTION_UP",Toast.LENGTH_LONG).show();
return true; // consumes the back key event - ActionMode is not finished
}
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
// handle your back button code here
Toast.makeText(this,"Back ACTION_DOWN",Toast.LENGTH_LONG).show();
return true; // consumes the back key event - ActionMode is not finished
}
return super.dispatchKeyEvent(event);
}
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<Button
android:id="@+id/standard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:text="Standard"/>
<Button
android:id="@+id/support"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:text="Support"/>
</LinearLayout>