why my back button doesn't function in google map activity ?
already include:
#Override
public void onBackPressed() {return;}
but still doesn't work.
You need this:
#Override
public void onBackPressed() {
super.onBackPressed();
}
Or this pre API level 5:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
// your implementation here
// finish(); - to exit the Activity
return true; // shows you consumed the event with your implementation
}
return super.onKeyDown(keyCode, event);
}
write
super.onBackPressed();
or
this.finish();
instead of return;
Related
I have a fragment which contains a webview.And the fragment is reused in all the four pages of a viewPager.What i want is, when the user clicks the back button i want the webView Fragment to go back to the previous tab of the current webViewFragment.And finally exit the activity.I can easily do this if the webView was inside an activity like this:
#Override
public void onBackPressed() {
if (mWebview.canGoBack())
mWebview.goBack();
else
super.onBackPressed();
}
But i cannot do this in a fragment because the fragment cannot override onBackPressed().How can i do this inside a fragment which is inside a viewPager
You can implement setOnKeyListener on webview to handle back button.
mWebview.setOnKeyListener(new View.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& mWebview.canGoBack())
{
mWebview.goBack();
return true;
}
return false;
}
});
If there is no previous page in your webview then it will trigger usual onBackPressed.
Hope it helps you.
This code is perfect for fragment webview backpress.
You can implement setOnKeyListener on webview to handle back button
mWebview.setOnKeyListener(new View.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& mWebview.canGoBack())
{
mWebview.goBack();
return true;
}
return false;
}
});
I have an activity where the whole screen is dedicated to sending one message. Being one EditText on the top half, and the SoftKeyboard always visible on the bottom half.
When i press back, the SoftKeyboard hides and i have to press back again to leave the activity.
The behavior that i'm struggling to get is : finishing the activity right away when i press the back button, instead of hiding the keyboard.
You can find this behavior in the twitter app for example, when writing a new tweet.
I tried with overriding the onBackPressed() function, but seems like when the keyboard is visible, the function is not called.
#Override
public void onBackPressed() {
finish();
}
Any help would be really appreciated!
So after trying many things, here something that worked :
Subclass EditText and override the onKeyPreIme() function to send a call back.
Here's the code for the subclass :
OnKeyPreImeListener onKeyPreImeListener;
public void setOnKeyPreImeListener(OnKeyPreImeListener onKeyPreImeListener) {
this.onKeyPreImeListener = onKeyPreImeListener;
}
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
if(onKeyPreImeListener != null)
onKeyPreImeListener.onBackPressed();
Log.d(TAG, "HIDING KEYBOARD");
return false;
}
return super.dispatchKeyEvent(event);
}
public interface OnKeyPreImeListener {
void onBackPressed();
}
Then in your activity for each of your TextView :
EditTextGraphee.OnKeyPreImeListener onKeyPreImeListener =
new EditTextGraphee.OnKeyPreImeListener() {
#Override
public void onBackPressed() {
Log.d(TAG, "CALL BACK RECEIVED");
MyActivity.this.onBackPressed();
}
};
editText.setOnKeyPreImeListener(onKeyPreImeListener);
new answer:
so apparently you don't receive the onBackPressed callback, but that doesn't mean you can't detect the keyboard closing.
Using the technique described here: How to check visibility of software keyboard in Android?
you can detect when the keyboard open/close, so when the keyboard closes you call finish();
deprecated, original answer:
simply override the back press event in the activity:
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
I assume that since the soft keyboard is visible probably an edittext has a focus. So you can catch the back pressed event by adding an OnEditorActionListener on that EditText and finish activity.
yourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP){
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){
finish();
}
}
return false;
}
});
You nee to extend EdtText class and implement onKeyPreIme method.
public class MyEditText extends EditText {
/* Must use this constructor in order for the layout files to instantiate the class properly */
public MyEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
}
#Override
public boolean onKeyPreIme (int keyCode, KeyEvent event)
{
// do your stuff here.
return true;
}
}
Override onBackPressed() method like this :
#Override
public void onBackPressed() {
hideKeyboard();
finish();
}
For hideKeyboard() function please search in the Internet .
I want to implement a KioskMode, I'm targeting only Android L, since this is a very specific App.
I already went through the process of setting my App as DeviceAdmin, and
DevicePolicyManager.isLockTaskPermitted(this.getPackageName()) already returns true.
I then start a LockTask via startLockTask().
Everything is fine, but when I hold down the backbutton, the app still exits the kiosk mode.
I have overridden onKeyPress to show a custom Dialog for unlocking the app, but this does not hinder android to automatically exit my lock task if the user holds down back.
I don't really know what to do at the moment and would be thankful for every input.
I now have overridden
#Override
public boolean onKeyDown(int KeyCode, KeyEvent event)
{
if(KeyCode == KeyEvent.KEYCODE_BACK)
{
BackDownButtonPressed = true;
if(VolDownPressed)
showTaskLockDialog();
return true;
}
else if(KeyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
VolDownPressed = true;
if(BackDownButtonPressed)
showTaskLockDialog();
return true;
}
return super.onKeyDown(KeyCode, event);
}
#Override
public boolean onKeyUp(int KeyCode, KeyEvent event) {
if(KeyCode == KeyEvent.KEYCODE_BACK)
{
BackDownButtonPressed = false;
return true;
}
else if(KeyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
VolDownPressed = false;
return true;
}
return super.onKeyUp(KeyCode, event);
}
#Override
public void onBackPressed()
{
return;
}
#Override
public boolean onNavigateUp() {
return true;
}
#Override
public boolean dispatchKeyEvent (KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
return true;
}
return true;
}
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//do something or nothing in your case
return true;
}
return super.onKeyLongPress(keyCode, event);
}
For the record, I am using a Samsung SM-T700 Tablet with Cyanogenmod CM12.1
Just to close this topic..
I couldn't figure out a perfect solution to this day. My current workaround is receiving an event if the user leaves the kiosk mode and just entering the kiosk mode again.
Sadly this leaves the user with 2 toasts saying "screen unpinned" and "screen pinned", which is unfortunate. But this satisfies my current needs.
Perhaps you need to override onKeyLongPress
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//do something or nothing in your case
return true
}
return super.onKeyLongPress(keyCode, event);
}
Not sure if it's helpful at all, but I wrote a blog about setting Kiosk Mode here:
http://www.sureshjoshi.com/mobile/android-kiosk-mode-without-root/
And also wrote sample code for it here:
https://github.com/sureshjoshi/android-kiosk-example
Not sure if you see any major differences between your code and mine, but I just tried to do a long press on a Samsung Galaxy Tab 4 running Android 5.0, and it won't exit the app.
Could it be something with rooting with Cyanogen?
If you don't have this in your code, perhaps add it in and check out if you see any problems:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Remove title bar and notification bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ComponentName deviceAdmin = new ComponentName(this, AdminReceiver.class);
mDpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
if (!mDpm.isAdminActive(deviceAdmin)) {
Toast.makeText(this, getString(R.string.not_device_admin), Toast.LENGTH_SHORT).show();
}
if (mDpm.isDeviceOwnerApp(getPackageName())) {
mDpm.setLockTaskPackages(deviceAdmin, new String[]{getPackageName()});
} else {
Toast.makeText(this, getString(R.string.not_device_owner), Toast.LENGTH_SHORT).show();
}
mDecorView = getWindow().getDecorView();
}
and
protected void enableKioskMode(boolean enabled) {
try {
if (enabled) {
if (mDpm.isLockTaskPermitted(this.getPackageName())) {
startLockTask();
mIsKioskEnabled = true;
} else {
Toast.makeText(this, getString(R.string.kiosk_not_permitted), Toast.LENGTH_SHORT).show();
}
} else {
stopLockTask();
mIsKioskEnabled = false;
}
} catch (Exception e) {
// TODO: Log and handle appropriately
}
}
I have the same problem: How to stop "holding the back button" from escaping "Lock Task mode" on Android 6+
The issue only occurs on my Android 7 tablet.
Running the app on my Android 6 tablet fixed the problem.
Could you add some code to illustrate your current work-around?
I need to minimize the application when back button is pressed.
I use following code to catch hardware back button click event
help me with the code of minimize on back key pressed
#Override
public boolean onKeyDown(int keyCode, keyEvent event) {
switch(keyCode) {
case KeyEvent.KEYCODE_BACK;
//minimize application
return true;
}
return super.onKeyDown(keyCode, event);
}
I think that you need to treat back event as home event. The code below is how I emulate home pressed when User press back button:
public void minimizeApp() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
This is a simple code to minimize the application
#Override
public void onBackPressed() {
this.moveTaskToBack(true);
}
try this code, this will minimize Activity.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
this.moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
or
If you want to close the activity use this.finish() method to close the current running activity. instead of this.moveTaskToBack(true);
Hello, I've been trying to integrate airpush in andriod but I've been having some issues.
#Override public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
As you can see is the andriod go back code for webview via history.
The problem is that I want execute this code when it has no more history, meaning when the application is about to close.
airpush.startLandingPageAd();
Any help will be greatly appreciated guys :)..
You need to edit your code, like this:
#Override public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
if(mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
else
{
//Your webview doesn't have any history here and you can close your app here.
//You can also execute your Airpush code here.
airpush.startLandingPageAd();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
I hope this helps.
Consider overriding onPause method of Activity for executing a piece of code before the activity hides.
#Override
protected void onPause() {
super.onPause();
airpush.startLandingPageAd();
}