0

I have an activity with some imageButtons in it. After I click on them i use setVisible(View.INVISIBLE); to have them gone. Now, when a user enter correct answer, a popup screen pops up with some info and OK button. I need to set all my imageButtons to be invisible when that popup window close. I tried to make some method:

private void removeImages(){
        b1.setVisibility(View.INVISIBLE);
        b2.setVisibility(View.INVISIBLE);
        b3.setVisibility(View.INVISIBLE);
        b4.setVisibility(View.INVISIBLE);
        b5.setVisibility(View.INVISIBLE);
        b6.setVisibility(View.INVISIBLE);
        b7.setVisibility(View.INVISIBLE);
}

and then call it onResume:

@Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        removeImages();

    }

But it does not work, it removes all my imageButtons as soon as I start that activity. How to do that after my popup windows closes, after I press OK button on that popup?

marjanbaz
  • 1,052
  • 3
  • 18
  • 35

1 Answers1

1

As per the Activity Lifecycle, onResume() is called before the Actviivty is in the foreground. You have a couple different options. You can use startActviityForResult() when you click an ImageButtonand check that value in onActivityResult() to set the Views how you wish. Or you could save a value in SharedPreferences to tell the Activity which Views to set invisible/visible in onResume()

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • I have 36 imageButtons so using startActivityForResult() sounds complicated and a lot of code. How to do the SharedPreferences thing? – marjanbaz Apr 08 '13 at 00:11
  • I just meant to use `startActivityForResult()` to return a value which tells `onResume()` to run `removeImages()`. This way it won't happen when `onResume()` is called on first creation of the `Activity` – codeMagic Apr 08 '13 at 16:46
  • And how to do that? I know how to return values like int using startActivityForResult() but I don't know how to return value which tells onResume() to run removeImages()? – marjanbaz Apr 08 '13 at 19:06
  • You actually don't even need to do that. If you only want to remove those images then you just call that method in `onActivityResult()`. It doesn't need to be in `onResume()` – codeMagic Apr 08 '13 at 19:08
  • I just need to remove them. I will try that. – marjanbaz Apr 08 '13 at 19:37
  • What should I put as second parameter in startActivityForResult()? If I return some int value I place something like Intent i = new Intent(FirstClass.this, Popup.class); and startActivityForResult(i, someIntVariable); – marjanbaz Apr 08 '13 at 19:58
  • I declare a constant at the class level like, `static final int MY_REQUEST = 0;` then use that constant. This has to be there but is really just used if you have more than one `Actviity` sending data back here – codeMagic Apr 08 '13 at 20:01
  • Can I still use i.putExtra stuff, cause I need it? – marjanbaz Apr 08 '13 at 20:03
  • You sure can. The only difference is that this `Activity` is awaiting a response from the next `Activity` when you call `setResult()` – codeMagic Apr 08 '13 at 20:04
  • I have similar problem to this. Can you check it out, maybe I can use onResult also for that? [link](http://stackoverflow.com/questions/15879945/how-to-save-points-after-popup-close-and-avoid-repetition-from-sqlite) – marjanbaz Apr 08 '13 at 20:15
  • You said I can send stuff using i.putExtra, but I can't send anything. – marjanbaz Apr 08 '13 at 21:31
  • Yes I did something wrong. I didn't see that I first called startActivityForResult and after that i.putExtra. When I switch their places everything worked perfectly. :) – marjanbaz Apr 08 '13 at 22:36