0

I have a TabHost set up with tabcontent. However, this tabcontent doesn't take up the entire screen and a different layout is loaded at the top. How can I change the image in the layout above from within the on create of a tab activity?

Thanks,

Sohel Mansuri
  • 564
  • 2
  • 6
  • 18
  • 2
    You just can't... If you really need that, you will have to let activity A know that it needs to change its ImageView. – K-ballo Jan 02 '13 at 21:29
  • @K-ballo, I have a Relative Layout on top, followed by tabcontent, followed by tabs. I can still see the relative layout becuase when a tab is clicked, I only change the tabcontent. – Sohel Mansuri Jan 02 '13 at 21:32

2 Answers2

0

You will not be able to access Activity A's view from Activity B; this is because it's very possible that the view will be destroyed when you launch Activity B.

What you can do however is somehow let Activity A know to change the ImageView when you come back. For example, you can have a static field or you can use setResult().

Oleg Vaskevich
  • 12,444
  • 6
  • 63
  • 80
  • I have a Relative Layout on top, followed by tabcontent, followed by tabs. I can still see the relative layout becuase when a tab is clicked, I only change the tabcontent. – Sohel Mansuri Jan 02 '13 at 21:31
  • So you have a `TabActivity` or just a regular `TabHost` that contains other `View`s within? – Oleg Vaskevich Jan 02 '13 at 21:37
  • my home activity extends TabActivity. When one of the tab is clicked, it changes the tab content to another activity (B) that extends TabGroupActivity which starts a ChildActivity. In this child activity, I want to change the imagivew of my home activity. – Sohel Mansuri Jan 02 '13 at 21:39
  • 1
    You should just be able to use `getParent()` to get the parent `Activity`. – Oleg Vaskevich Jan 02 '13 at 21:41
  • 1
    That being said, I wouldn't use `TabActivity`; I would use either `View`s or `Fragment`s which would be simpler, run faster and take up less memory (and is now the recommended approach for these designs). – Oleg Vaskevich Jan 02 '13 at 21:42
  • Can you explain a bit more? What would I do with the Activity object? Get the window? – Sohel Mansuri Jan 02 '13 at 21:42
  • I understand, however, I need to design it for 2.2 and fragments don't do everything I want. – Sohel Mansuri Jan 02 '13 at 21:43
  • Once you get the `Activity` object, you can just use findViewById() to get your `ImageView`; i.e. `ImageView iView = (ImageView) getParent().findViewById(R.id.yourImageView)`. Also, `Fragment`s work down to Android 1.6 with the compatibility library. – Oleg Vaskevich Jan 02 '13 at 21:43
  • Well, there are a lot of solutions to this on the internet; check this http://stackoverflow.com/q/3134783/832776 – Oleg Vaskevich Jan 02 '13 at 21:50
  • I want to do the opposite. I want to click on something in the tabcontent and change the imageview that is on top of the tabcontent. – Sohel Mansuri Jan 02 '13 at 21:53
  • it is giving me a NULLPOINTEREXCEPTION. I think getParent() is not getting HomeActivity. How can I make a Activity object and specify HomeActivity? – Sohel Mansuri Jan 02 '13 at 22:23
  • Make sure you're not within an inner or anonymous class... do `MyTabActivity.this.getParent()` if needed. – Oleg Vaskevich Jan 03 '13 at 02:26
0

I see Three ways:

1.Share the ImageView between two activities. To do that, you can put this element as private and create a getter. Then, you just have to access this element and change the element. I'm not sure that will work.

2.preferred way Load your image in onResume from a remote source. Then, you juste have to change this remote path from your second activity.

3.previous response

Stephane
  • 171
  • 1
  • 10
  • The first will not work because `View`s are constructed in their own `Context`; you can't just "share" views because then you'll leak the hosting `Activity`; you have to create a new one. – Oleg Vaskevich Jan 02 '13 at 21:33