bigb055
bigb055

Reputation: 308

Change between alternative layouts without recreating activity

Initially I had a single layout for my activity and used the following attributes to avoid re-creating the activity and thus sending a new request to the database on screen rotation:

ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize

Later I decided to use an alternative layout for landscape mode. The problem was my app never switched to that alternative layout because of the above mentioned attributes. So, I removed them, and it started switching between landspace/portrait layout, but now I'm facing the same issue that the activity sends a new request to the DB on each recreate.

How can I use alternative layouts and avoid the activity being recreated?

    protected override void OnCreate(Bundle savedInstanceState)
            {
                AllLinesFromBatch = new List<WhseActivLine>();
                Orders = new List<WhseActivHeader>();

                base.OnCreate(savedInstanceState);
                SetContentView(Resource.Layout.NextOrder);

                var backBtn = FindViewById<Button>(Resource.Id.buttonBack);
                backBtn.Click += BackBtn_Click;

                var startBtn = FindViewById<Button>(Resource.Id.buttonStart);
                startBtn.Enabled = false;
                startBtn.Click += StartBtn_Click;

                notificationsArea = FindViewById<TextView>(Resource.Id.notificationsNextorder);
                notificationsArea.Text = string.Empty;

                var dsHeaders = Utility.WsHueckmann.GetDataWhseActivHeaders(PickActivity.CheckedZonesList.ToArray(), LoginActivity.User);

....other code.....
            }

In the above code the line

var dsHeaders = Utility.WsHueckmann.GetDataWhseActivHeaders(PickActivity.CheckedZonesList.ToArray(), LoginActivity.User)

is where the call to DB takes place.

Upvotes: 1

Views: 308

Answers (2)

MeLean
MeLean

Reputation: 3421

I don get full picture of what you are trying to do but I think you should override the onConfigurationChanged of your launcher activity as described here

Here you can find an answer, how to use different resources for different screen orientation

Upvotes: 1

Pouya Heydari
Pouya Heydari

Reputation: 2616

if problem is only the DB request ,I think the easiest way is making an Application global variable , and do something like this in your activity :

if (globalDsHeaders == null){
var dsHeaders = Utility.WsHueckmann.GetDataWhseActivHeaders(PickActivity.CheckedZonesList.ToArray(), LoginActivity.User)
globalDsHeaders = dsHeaders
}
else {
dsHeaders = globalDsHeader
}
....

let me know if this is helpful or not.

Upvotes: 2

Related Questions