-1

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>
user101
  • 417
  • 1
  • 4
  • 17

3 Answers3

3

Just override activity method and finish explicitly

@Override
public void onBackPressed() {
finish();
}

UPDATED2

Yes, this doesn't work for such case. But I've tried few solutions and one of them did work. You need to extend EditText and override method:

@Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            ((Activity)getContext()).finish();
            return true;
        }
        return super.onKeyPreIme(keyCode, event);
    }

Note: don't forget to replace your previous EditText in layout xml with new extented class

Yazazzello
  • 5,336
  • 1
  • 17
  • 21
1

call the following function on your backbutton wherever and whatever:

 yourMethod()   
    {
// close keyboard
    InputMethodManager mgr = (InputMethodManager) TempActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                mgr.hideSoftInputFromWindow(searchBox.getWindowToken(), 0);

    //close activity
    YourActivity.finish(); 
 }

for backpressed of android device do as:

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub

yourMethod();
YourActivity.finish();
    }
Androider
  • 3,691
  • 2
  • 11
  • 23
0

Activity.onBackPressed() won't work, because it's being called after (and if) nothing else handled the back button (which IME does).

Override Activity.dispatchKeyEvent() instead:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        finish();
        return true;
    }
    return super.dispatchKeyEvent(event);
}
Ivan Bartsov
  • 16,024
  • 6
  • 54
  • 57