Question

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

       }
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top