0

I want to show an activity in an Android application I'm writing with Eclipse. This is the way I do that:

Intent intent = new Intent(HomeActivity.this, SecondActivity.class);
intent.putExtra("query", query);
startActivity(intent);

How can I handle the events of the new activity just where I'm showing it? I have made an event listener for the second activity. If I want to handle the events I should make an instance of the class "SecondActivity" like this:

  SecondActivity act = new SecondActivity();
  act.itemselectedlisteners.add(new ListItemSelectedListener() {

        @Override
        public void onItemSelected(String key) {
            // TODO Auto-generated method stub
        }
  });

But then I don't know how to show the dialog. How do I combine these codes?

4

1 回答 1

3

You should never instantiate Activities with new. Furthermore there is no point in setting event listeners in the calling Activity as it will be brought to background (meaning in not-running mode) while the second one is showing.

The correct way to do what you want in Android is to use startActivityForResult and have the second activity return to the first one appropriate results when the events you are interested in happen. This will also decouple your activities further, because the first one will not need to know anything about the events happening in the second one.

于 2013-07-07T08:14:14.683 回答