Android back button does not work - java

I am using cocos2dx to make a small game and in the activity of my game i give the following functions to handle back button.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return super.onKeyDown(keyCode, event);
}
#Override
public void onDestroy()
{
android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();
}
On pressing back button i get the following warning in my logcat
Can't open keycharmap file
Error loading keycharmap file '/system/usr/keychars/qtouch-touchscreen.kcm.bin'. hw.keyboards.65538.devname='qtouch-touchscreen'
The call doesn't reach onKeyDown or onDestroy functions.
Please tell me why this warning is caused and why i cannot handle the android back button.
The functions work fine on my java android project but not in my cocos2d-x project

It is been handled here in the file Cocos2dxGLSurfaceView.java
change it to below, where myActivity is the cocos2dActicity
case KeyEvent.KEYCODE_BACK:
AlertDialog ad = new AlertDialog.Builder(myActivity)
.setTitle("EXIT?")
.setMessage("Do you really want to exit?")
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
((Cocos2dxActivity)myActivity).finish();
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).create();
ad.show();
return true;
case KeyEvent.KEYCODE_MENU:

To handle back button pressing you need to redefine onBackPressed() method of your activity, not this two methods.

Have you enabled the touch? If not then please enable it and Hope, it'll sort-out your problem.
I assume that's button in your game screen.

Just your apps implements for override method for onKeyDown,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Here to implements for your code.
Log.d(TAG, "KEYCODE_BACK");
}
return super.onKeyDown(keyCode, event);
}

Here's an update for Cocos2d-x version 3+
This has been answered simply (and works) here
as well as a slightly less complete youtube here

Related

How to finish() an activity with SoftKeyboard visible with onBackPressed()

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 need to minimize the android application on back button click

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);

How to trigger an event on Dialog dismiss Android?

I have a custom dialog which extends the Dialog Class, I would like to bind an event to execute some code after the Dialog is closed when the user presses the BACK button of the device. How can I do that? I found a post where someone says that the .addWindowListener() should be used when working with Dialogs and other Window widgets. But the dialog class doesn't have the addWindowListener method, so I cannot use it. Is there another way without using fragments cause I shouldn't re-write the MyCustomDialog class?
This is the code:
public class MyCustomDialog extends Dialog {
public MyCustomDialog(Context context, int layoutResourceId) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(layoutResourceId);
}
}
Thanks for the attention!
EDIT: i found this on the android developers site, is there a way to use it with MyCustomDialog class?
onDismiss DialogInterface
Since you are extending android Dialog class you can implement a Dismiss Listener in your Activity's and set it when you create the Dialog, and then in the listener implement any functionality you want depending on the button that was used to dismiss the Dialog.
Hope this will solve your problem.
Edit You can use dialog.setCanceledOnTouchOutside(false); which will stop closing the dialog if you touch outside of the dialog.
Something like,
Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(false);
OR Alternatively
Override onTouchEvent() of dialog and check for action type. if the action type is
'MotionEvent.ACTION_OUTSIDE' means, user is interacting outside the dialog region. So in this case, you can dimiss your dialog or decide what you wanted to perform.
view plainprint?
dialog.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_OUTSIDE){
Toast.make(getApplicationContext(), "TOuched outside the dialog", Toast.LENGTH_LONG).show();
this.dismiss();
}
return false;
}
});
And for back press you can do dialog.setCancelable(false); which will prevent dialog getting cancelled from backpress event.
OR you can alternatively override setOnKeyListener event and put your own code into it.
Edit
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
#Override
public boolean onKey(DialogInterface arg0, int keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
dialog.dismiss();
}
return true;
}
});
Happy Coding!!!
You need to Override onBackPressed inside Dialog class. Also make sure to close dialog after override OnBackPressed .
Try this
public class MyCustomDialog extends Dialog {
public MyCustomDialog(Context context, int layoutResourceId) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(layoutResourceId);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
dismiss(); // make sure to call dismiss to close dialog
// put your code here
}
}
If you want to trigger an event if user clicked/touched outside the dialog and close it or used the back button to close then use
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
//your trigger goes here
Toast.makeText(IntroductoryActivity.this, "on cancel", Toast.LENGTH_SHORT).show();
}
});
But if you want to trigger an event if something dismisses the dialog, like some other event then use
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
//your trigger goes here
Toast.makeText(IntroductoryActivity.this, "on dismiss", Toast.LENGTH_SHORT).show();
}
});

How to handle the 3 hard Keys in Android

There are the 3 basic keys in an Android device.
1. Home button (middle button)
2. Menu button (left most button)
3. Back Button (right most button)
I'm trying to create a code the use will press the Back button and a AlertDialog will appear before return to the previous activity.
How can I handle the back button and do I have to insert it on the onCreate on the Java File of an Activity?
Thank You!
All (4) buttons are handled differently.
The home button and running apps buttons are completely outside your control.
To get the BackButton press, override this function.
#Override
public void onBackPressed(){
//Do Stuff
}
To get the Menu button key press, use the following
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
doSomething();
return true;
}
return super.onKeyDown(keycode, e);
}
After you show the user your alert dialog, you can either send a real BackPress to the system using super.onBackPressed(), or you can manually finish() your activity.
#Override
public void onBackPressed(){
AlertDialog.Builder build=new AlertDialog.Builder(currentactivity.this);
build.setMessage("Are you sure you want to return?");
build.setCancelable(false);
build.setTitle("Note");
build.setPositiveButton("yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
Intent i =new Intent(currentactivity.this,anotheractivity.class);
startActivity(i);
//or just type finish();
}
});
build.setNegativeButton("no", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.cancel();
}
});
AlertDialog alert=build.create();
alert.show();
}

BACK key does not finish activity

I am running a service that starts a new activity when specific
applications are launched.
For example, when I launch sms application, my service detects it by
checking a top activity package name and starts a new activity.
But the problem is that after starting a new activity, when I finish
that activity and press BACK button from sms application to go back to
Home screen, it does not finish my sms application.
Even though the screen is at home(launcher), when I check top activity
name, sms app is running as the top activity, which means sms app is
not finished after pressing BACK button.
I use Intent.FLAG_ACTIVITY_NEW_TASK intent flag for starting a new
activity and finish() to finish it. Does anyone have an idea why my
BACK button does not finish sms application in this case?
thanks,
777
From what I've seen, the back button will halt the current activity, whatever it's doing. If you absolutely need to finish it off, take a look at the lifecycle of an activity, and perhaps put some code into the onPause() and onStop() functions.
OK if it doesnt workout.. try overriding the OnBackPressed method
and put finish() in that.. hope this helps
If you want to finish the activity when user navigate to back through device So you can use this code it is more helpful.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
I did something like that in my activity:
#Override
public void onBackPressed(){
AlertDialog.Builder alert = new AlertDialog.Builder(ctx);
alert.setTitle("Wylogowanie i wylaczenie.");
alert
.setMessage("Exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//DO SOMETHING YOU NEED eg. destroy/close DB, stop services, and move the task to back
db.getWritableDatabase(); //getDB
db.destroyDB(context); //Destroy it
db.close(); //and close
stopService(new Intent(getBaseContext(), SVCKeepLogged.class)); //stop some service
moveTaskToBack(true); //hide activity (this do not kill it - you can use finish() here)
//finish();
}
})
.setNegativeButton("NOOO!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
//Keep app alive.
dialog.cancel();
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
}
try this
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
// TODO
}
#Override
public boolean onOptionItemSelected(MenuItem item){
if(item.getItemId() == android.R.id.home){
finish();
}
}

Categories

Resources