1

In my activity I have a custom view which I add on top of the RootView. Inside the custom view I have two Radiobuttons. When I remove the activity from the RootView the state of the radio buttons is gone. How can I save the state of the radiobuttons without the activity lifecycle?

Here is my code for better understanding the question:

class MyCustomView : View

var myCustomView: View = View.inflate(context, R.layout.custom_view, null)
val closeButton by lazy {
    myCustomView.findViewById(R.id.btn_close) as AppCompatImageView
}

 init {
    val rootView = (context as Activity).window.decorView.findViewById(android.R.id.content) as ViewGroup
    rootView.addView(myCustomView)
    setCloseButtonClickListener(closeButton, rootView)
}

fun setCloseButtonClickListener(closeButton: AppCompatImageView,
                                rootView: ViewGroup) {
    closeButton.setOnClickListener { onCloseButtonClick(rootView) }


fun onCloseButtonClick(rootView: ViewGroup) {
    rootView.removeView(myCustomView)
}

When the user click the close button, the view will removed / closed from the root view

dudi
  • 5,523
  • 4
  • 28
  • 57
  • Explain `When I remove the activity from the RootView` . Provide code for it . – ADM Aug 13 '18 at 10:21
  • @ADM I've updated my question – dudi Aug 13 '18 at 10:32
  • You are removing a view from decor view . This has nothing to do with `onSavedInstanceState()`. You can just save the data globally . – ADM Aug 13 '18 at 10:38
  • @ADM do you mean I have to save the data with SharedPrefs? – dudi Aug 13 '18 at 10:45
  • No i mean save it Globally in a variable `onSavedInstanceState` have different purpose . Read [This thread](https://stackoverflow.com/questions/20831826/when-exactly-are-onsaveinstancestate-and-onrestoreinstancestate-called) and figure it out whether you need it or not . – ADM Aug 13 '18 at 10:47
  • Can you explain it with a peace of code? – dudi Aug 13 '18 at 10:55

0 Answers0