6

I have 2 Activities: A,B.Layout of Activity A,has a viewgroup that user changes it's content.In Activity B,I have to show that viewgroup again,without any change,it must be a real copy of that viewgroup,so texts,colors,dimensions,order(of childs) and ... must be same.So I can not use Layout Inflater.Is it possible without creating classes of the type of childrens of that viewgroup and change properties?Because if I have more than 2 Activity with different viewgroups,it is very difficult to show viewgroups of each activity in last Activity.

Also I can not remove those viewgroups from their parents.

Student Student
  • 960
  • 2
  • 14
  • 24

5 Answers5

5

If their content will be the same, there is no point in having two different activities. You can dynamically change one activity' s content and the behavior will be the same as there were two activities. If it is really necessary, then you will have to save all the needed information to reconstruct the activity again and pass it to the newly created activity. Take a look at this.

Community
  • 1
  • 1
Alpay
  • 1,350
  • 2
  • 24
  • 56
4

No easy way to do this. You cant move view's between activities. So you have several options:

  • create a bitmap of viewGroup and show in in new activity (doesn't work is you need editable copy)
  • save view's hierarchi state in old activity and recreate it in new one (using fragments makes it easier).
  • do not create new activity. Just change some UI parts in old one not touching target ViewGroup.
Leonidos
  • 10,482
  • 2
  • 28
  • 37
1

Create a class that holds the configuration of your view group. Let this configuration class hold all the information related to your ViewGroup. It will hold the texts, the colors the dimensions the order and everything user has changed. Pass object of this class from Activity A to Activity B and using this, reproduce the same view by inflating the same layout.

Hope that helps.

And to answer your question, there is no other easy way of doing this.

Sudarshan Bhat
  • 3,772
  • 2
  • 26
  • 53
1

For the ViewGroup that needs to be shared, refactor it into a Fragment named C. Then create Fragments for the sections of Activity A and B minus this shared portion. Then contain all of these Fragments within a new containing Activity (you will no longer need Activities A and B).

Fragment A and C will be the new Activity A. Fragments B and C will be the new Activity B. To transition from the first state to the second, do a FragmentTransaction adding Fragment B and removing Fragment A. Remember to add this transaction to the back stack so the back button will bring you back to the first state.

user697495
  • 590
  • 1
  • 3
  • 9
1

Just a general doubt ? why would you want to go for two activities why not use two fragments assign to the same view use the underlying activity to store all the changes happening in one of the fragment (View) and when the user is passing to the other view just send in the parameters to the second fragment . Thereby you are emulating to the user that it is two activities but in reality its just two fragments controlled by a single activity .

Chris
  • 798
  • 1
  • 9
  • 15