1

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.

bigb055
  • 198
  • 3
  • 14
  • Did you try to manage things in onConfigurationChanged method? – MeLean Jan 14 '18 at 17:32
  • @MeLine can you be more specific? How should onConfigurationChanged look like in my case? Sorry, but I've never used that method before – bigb055 Jan 14 '18 at 17:40

2 Answers2

2

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.

Pouya Heydari
  • 2,496
  • 26
  • 39
1

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

MeLean
  • 3,092
  • 6
  • 29
  • 43
  • Ok, but how do I change to the alternative layout inside onConfigurationChanged? – bigb055 Jan 14 '18 at 17:50
  • I think I get it but my two layouts have the exact same name "**NextOrder.axml**" just the folders are different. It won't work by simly calling `SetContentView(Resource.Layout.NextOrder)` cause it does not know which one to use. Or am I wrong? – bigb055 Jan 14 '18 at 18:03