2

In an Android app, I have two fragments:

  1. a fragment with a ListView of items

  2. 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:

  1. Is there a way to automatically enable/disable listeners based on their Fragment visibility?

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

       }
Javide
  • 2,477
  • 5
  • 45
  • 61

1 Answers1

2

You can try two things

  1. Set the ImageView to consume touch events.

    ImageView.setClickable(true);

  2. When pushing the new fragment disable touch events on the ListView.

    ListView.setClickable(false);

If you want to know how to know when the fragment with the ImageView is removed try setTargetFragment. Take a look here: https://stackoverflow.com/a/13733914/935421

Community
  • 1
  • 1
Juangcg
  • 1,038
  • 9
  • 14
  • I decided to use the first solution as I didn't manage to make the second one working. The ImageView is now not clickable. However, I noticed something odd. If I tap the ImageView with multiple fingers sometimes this info appears in LogCat: "ACTION_DOWN before UnsetPressedState. invoking mUnsetPressedState.run()". It doesn't seem to create any issue though. – Javide Sep 04 '13 at 01:33