So in an app I'm messing around with I programmatically create some buttons. Then I setContentView(); to a different page. Then if I come back to the original page (on which I placed the programmatically created buttons), they no longer exist. I guess this makes sense as I am cal开发者_如何学Goling setContentView(R.layout.main); again which is just the original xml file with no data pointing to the buttons. So is there a way to have the buttons persist over such screen transitions? Thanks.
Why not use separate activities for each page? You'll get back stack behavior and preservation of activity state automatically.
Use
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)
in your Intent when you are starting the Activity which contains the Buttons.
I think this will solve your problem
精彩评论