0

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

}
Falcon
  • 1,317
  • 1
  • 13
  • 30

5 Answers5

2

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

Jiang Qi
  • 4,450
  • 3
  • 19
  • 19
  • Thank you so much for the attention to such details (I noticed that before but didnt thought much about it)! Tested and this solved the problem. – Falcon Jan 11 '12 at 19:33
0

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();
    }
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95
  • 1
    I really want to leave it to the system to decide where to go back. I don't want to terminate my activity. (Maybe the "exit" in my question subject line is confusing) – Falcon Jan 11 '12 at 08:56
  • here same topic discussed http://stackoverflow.com/questions/2092951/how-to-close-android-application – Parag Chauhan Jan 11 '12 at 11:03
  • I looked and that is not it. the problem is rather minor. please see answer. – Falcon Jan 11 '12 at 19:35
0

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.

jency
  • 377
  • 2
  • 3
0

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>
BobDroid
  • 1,898
  • 5
  • 19
  • 39
  • this looks like an intriguing technique... but i mean back in the context of android view/activity stack – Falcon Jan 11 '12 at 09:04
0

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

JohnCookie
  • 661
  • 3
  • 7