1

I want to use 1 view instance in multiple activities. (Ignore the reason please) I am curious about the advantages and disadvantages of the following choices:

  • MutableContextWrapper for the Custom View (for example: RelativeLayout) and switching context to another activity when that is shown. So, moving view from activity to activity.
  • Using fragments.
frankish
  • 6,738
  • 9
  • 49
  • 100
  • Have you tested the feasibility of this? I mean, a `View` can have only a single parent at a time. I'd imagine that coordinating the detach/attach alone would be rather cumbersome for any non-trivial setup. – Mike M. Sep 19 '19 at 10:18
  • @MikeM. Yes. It seems like it is working and it is not preventing related activity from being destroyed. I keep the View in an outside singleton class which also keeps MutableContextWrapper. Whenever I need to attach that view to somewhere else, I remove the view from the parent first, then change the baseContext of that view's MutableContextWrapper. It works but this does not mean that it has no disadvantages, that is why I would like to hear about your experiences. (I know that keeping it in a singleton and not correctly managing it may easily cause leaks) – frankish Sep 19 '19 at 10:56
  • I need to do this in Flutter, to host a fragment in the View class that brings it to flutter app – Mostafa Mahmoud Apr 06 '23 at 07:03

0 Answers0