In an Android app, I have two fragments:
a fragment with a ListView of items
a fragment with an ImageView
Through callback onListItemSelected, when a user clicks on a ListView item, MainActivity pushes the ImageView on the stack and the fragment with the image appears on the screen. At this point I would expect that, since the ListView fragment is no longer visible, any events associated to this fragment are no longer fired. This is not the case. If I touch the ImageView, the listeners of the ListView items still fire.
Two questions:
Is there a way to automatically enable/disable listeners based on their Fragment visibility?
If not, I guess the way to go would be to disable the ListView fragment view and then re-enable it when the backButton is pressed. How can I capture the backButton event in the MainActivity to re-enable a previously disabled view?
public class MainActivity extends FragmentActivity implements ListViewFragment.Callbacks {
[...]
public void onListItemSelected(String str) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.addToBackStack(null); fragmentTransaction.replace(R.id.listView, f); fragmentTransaction.commit(); // disable listView //View lw = getSupportFragmentManager().findFragmentById(R.id.listView).getView().findViewById(R.id.my_listView); //lw.setEnabled(false); }