Sunday, September 23, 2012

Back to basic

Let's look at activity basic - How we can maintain a view as android recycle it's view either when it switch from vertical view to horizontal view or vice-versa.

Within activity at java code level
public class UIACTIVITY extends Activity {

//There are two views we have in this app
int activityLayoutA = R.layout.ViewA;
int activityLayoutB = R.layout.ViewB;
int currentActivity = R.layout.ViewA; //defaultview

//single method to view which view is to be constructed
public void switchAppWideViews(int LayoutView){
setContentView(LayoutView);
if(LayoutView == activityLayoutA){
//LAYOUT ACTIVITY A SETTINGS HERE
}

if(LayoutView == activityLayoutB){
//LAYOUT ACTIVITY B SETTINGS HERE
}
currentActivity = LayoutView; //set a current view

}


@Override
    public void onCreate(Bundle savedInstanceState) {
//Standard create view on switch
           super.onCreate(savedInstanceState);

//savedInstanceState will be null if it is launched for the first time
        if(savedInstanceState==null){       
        switchAppWideViews(authenLayout);
        }else{

//activity already launched

                int cLayout = (Integer) savedInstanceState.get("CURRENTLAYOUT");
        switchAppWideViews(cLayout);


        }


@Override
    protected void onSaveInstanceState(Bundle outState) {
//Save current layout before view is destroyed
    outState.putInt("CURRENTLAYOUT", currentActiveLayout);
    super.onSaveInstanceState(outState);
    }



}

No comments:

Post a Comment