android - Restart activity as e.g. orientation change does -
i want app support different layouts right-handed , left-handed users. after changing respective preference want restart activity in same way restarts when e.g. orientation changes.
what tried far:
1.
intent intent = getintent(); finish(); startactivity(intent); this not store , load saved instance state
2.
view cv = findviewbyid(android.r.id.content); sparsearray<parcelable> state = new sparsearray<parcelable>(); cv.savehierarchystate(state); setcontentview(desiredcv); cv = findviewbyid(android.r.id.content); cv.restorehierarchystate(state); even many things aren't should be.
i think in end figure out how change layout without restarting easier in same way system-defined configuration changes.
you use fragments , programmatically. following same way rearrange dinamically elements in ui think complicated maintain.
onsaveinstancestate() not called on activity being finish-ed. , i'm not aware of way let android handle you.
the solution may create 2 different layout files. programmatically select right 1 in oncreate() method based on preference value has been updated.
if 2 layouts use same ids views need restore, can implement 2 methods save these states in bundle , retrieve these states bundle. when want change layout, start new activity proper intent (telling activity layout load); in bundle associated intent save need , retrieve in oncreate() update views. don't need duplicate code in case use layout dx or sx; can use same code since 2 layouts use same ids.
in way still have 1 activity , can reuse code. , same code use restore state between layout changes can still used onsaveinstancestate() , onrestoreinstancestate(), since, again, ids same.
to make short, use bundles.
Comments
Post a Comment