Falcon
Falcon

Reputation: 1367

My Android App Does Not Exit When Pressing Back Button

I am new to Android programming and I wrote an app implementing only the Activity.OnCreate method. I expected to go back to the previous view (which has to be the home screen due to the basic intent-filter I have) by pressing the BACK button but it does not. I managed to do that only by pressing the HOME button. (I have tested my app on multiple devices and it behaves the same.) Has everybody else went into the same problem? What is the solution? (the git repo of my app is here: https://github.com/falcondai/android_lab1)

The following is the Activity I failed to back out properly (the BACK key works fine for all other activities):

public class NameGetterActivity extends Activity 
{
    private static final String TAG = "NameGetterActivity";

    private EditText name_fld;
    private Button submit_btn;
    private Button test_btn;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.name_getter);

        name_fld = (EditText)this.findViewById(R.id.editText1);
        submit_btn = (Button)this.findViewById(R.id.button1);
        test_btn = (Button)this.findViewById(R.id.button2);

        submit_btn.setOnClickListener(
                new OnClickListener() {
                    public void onClick(View v) {
                        Log.d(TAG, name_fld.getText().toString());

                        Intent i = new Intent(NameGetterActivity.this, HelloWorldActivity.class);
                        i.putExtra("name", name_fld.getText().toString());
                        startActivity(i);
                    }
                }
            );

        test_btn.setOnClickListener(
                new OnClickListener() {
                    public void onClick(View v) {
                        Log.d(TAG, "sensor activity button pressed");

                        Intent i = new Intent(NameGetterActivity.this, SensorActivity.class);
                        startActivity(i);
                    }
                }
            );
    }

}

Upvotes: 0

Views: 3557

Answers (5)

Jiang Qi
Jiang Qi

Reputation: 4448

your layerout xml file use MediaController as layout root, replace it with LinearLayout

Upvotes: 2

JohnCookie
JohnCookie

Reputation: 671

I don't understand your problem clearly, but here maybe some tips helpful to you. First, make your HomeActivity's launchMode="SingleTask" in manifest.xml, this will make your HomeActivity only has single instance.

Second, I suppose that you just want to get some info(like name) for HomeActivity to use, so try the method startActivityForResult(); then return by call setResult(Intent); finish(); in your NameGetterActivity and process the info in onActivityResult() in HomeActivity(implement this method just like onCreate onStart...).

(I think maybe your situation is like a Login Activity or a Regist Activity, so the second way is useful in this situation.)

Upvotes: 0

BobDroid
BobDroid

Reputation: 1898

If you use HTML pages in your project means this code will helpful for you.If you want to go back means just include this javascript in your HTML page it will definitely work in all android emulators.

<a onclick="history.back(-1)" data-role="button" data-icon="back" data-theme="e" style="padding:8px;"></a>

Upvotes: 0

jency
jency

Reputation: 377

Please check whether you have finished your previous activity before starting the current activity from where you have pressed the back button.

ie;

if you call finish(); before the function startActivity(intent);, The previous activity will get finished and wont be there in the stack of activities any more.

Upvotes: 0

Parag Chauhan
Parag Chauhan

Reputation: 35936

You should override finish()

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
       this.finish();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

Then invoke this method like this:

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

Upvotes: 0

Related Questions