Well my question may be a bit immature but I'm really wandering about it.
How can I access the layout of activity A from the class of activity B. i.e I want to change the image in xml
file of Activity A from activity B.
Is it possible? Kindly help.

- 346
- 1
- 3
- 19

- 299
- 3
- 14
-
have a look http://stackoverflow.com/questions/22901586/how-to-open-xml-layouts-from-another-activity-layout-guide – Sukan Apr 07 '14 at 09:27
3 Answers
You don't need to do it in such a way. I suggest you to look at the startActivityForResult. Start Activity B
, do all the needed work there and once you are done, you'll be returned to the Activity A
and considering the data you get at the onActivityResult, you can change the image at the content view of Activity A

- 11,166
- 2
- 35
- 45
-
Thats what I was looking for. I didn't know about startACtivityForResult..Hopefully it will solve my problem now.Thankss :) @nikis – Nauman Aslam Apr 07 '14 at 09:47
No you can't access views of one Activity in another. You can Navigate to another Activity and change the image there.
You set a layout for each Activity and Each Activity has a life cycle.

- 132,755
- 26
- 225
- 256
-
-
1
-
Thankyou but how can I do that without opening that activity..i.e can I use intent and putexrtra to just change the image in other class without launching it? any example plz? – Nauman Aslam Apr 07 '14 at 09:29
-
@NaumanAslam you can't. You need to navigate to Activity B and then change the image there. At a time only one Activity is displayed on the screen. – Raghunandan Apr 07 '14 at 09:30
-
2@NaumanAslam : **"...change the image in other class without launching it?"** Your use of the words "other class" describes your problem exactly. An Android `Activity` is not just a "class" it is a special case "class" designed to be a UI framework - if it's not visible or has never been launched then it has no purpose. Further to this, if Activity A starts Activity B then you have no guarantee that Activity A will still be alive when Activity B is visible - the OS might destroy Activity A if system resources are low - unlikely but possible. – Squonk Apr 07 '14 at 09:43
-
@NaumanAslam and do read the answer by Raghav Sood http://stackoverflow.com/questions/14956018/can-i-create-the-object-of-a-activity-in-other-class. Activity class is not a normal java class. this will help in future although directly not related to your issue. – Raghunandan Apr 07 '14 at 09:45
-
@Raghunandan I m getting the concept behind this now and though for the time startActivityForResult() was required but it'll definitly help me in future development.Thanks alot for the help – Nauman Aslam Apr 07 '14 at 09:57
-
@NaumanAslam even in that case you navigate to the appropriate activity but you cannot access view of activity b in activity a – Raghunandan Apr 07 '14 at 10:18
You Can't modify A's Image at a running time becasuse A will be in pause mode when B is in front. You need to startActivityForResult();(A ----> B);
And in ActivityForResult()
method you need to right your Handler logic to change the Image.
In B activity finished you need to set the Result.

- 715
- 6
- 22