I am starting my softkeyboard in my ActivityB with this lines in my manifest
<activity android:name=".ActivityB"
android:label="@string/title_activity_activity_b"
android:windowSoftInputMode="stateAlwaysVisible" >
</activity>
Now I would like that when I press a back button I exit(finish) the activity immediately. I would like to press back button only once to exit the activity.
Right now I have to first press back button once to lower the soft-keyboard and press the back button again for the second time to exit activity.
Is this possible and how can I do that? I tried various common techniques from this site but none of them worked. I use LG L50 for development.
EDIT: I am extending activity and not appcompatactivity
EDIT 2: The program works now, here is the code
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}}
MyEditText.java
package com.example.actionbarimagetest;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
public class MyEditText extends EditText implements OnClickListener {
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyEditText(Context context) {
super(context);
}
public MyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void FlashBorder()
{
//do some custom action
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText txt = (EditText) v;
txt.selectAll();
}
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
System.out.println("KEYCODE BACK");
((Activity)getContext()).finish();
return true;
}
return super.onKeyPreIme(keyCode, event);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<com.example.actionbarimagetest.MyEditText
android:id="@+id/edtTaskName"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</LinearLayout>