Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling Display Size change

I am migrating my app target SDK Version to android oreo.

One of the changes which might affect the app is that if the Display size of the device is changed then the app targeting Android Nougat and above will be notified as if there is a change in orientation of the device. Below is the source.

When the device density changes, the system notifies running apps in the following ways:

If an app targets API level 23 or lower, the system automatically kills all its background processes. This means that if a user switches away from such an app to open the Settings screen and changes the Display size setting, the system kills the app in the same manner that it would in a low-memory situation. If the app has any foreground processes, the system notifies those processes of the configuration change as described in Handling Runtime Changes, just as if the device's orientation had changed. If an app targets Android 7.0, all of its processes (foreground and background) are notified of the configuration change as described in Handling Runtime Changes.

Now my app has a longrunning process which runs in an Async TSak in an Android Activity. Which means that the App has an AsyncTask which is inside the Activity Code.

I create a Dialog box on the start of the Async Task and hide it when the Async Task has done its work.

Now suppose the users start the task and then goes to the setting and changes the Display Size then return back to my app then the dialog box of the app is gone by the Async Task is still performed till the end which mean that the user might think that the app has finished the task but whereas the app would be actually performing the task. But my Activity is also responding as if restarted except that Async Task is running.

My app's orientation locked to portrait.

How should I handle such a scenario? Any help is appreciated. Thanks!

EDIT: This is the scenario

1) App Start

2) Main Screen

3) Users Press A Button

4) AsyncTask inside Main Screen Started

5) A Dialog Box is shown with the progress of task

6) User Minimizes the App and Goes to setting

7) Changes the Display Size/Density or changes the Font Size of the device

8) My App is called by the OS in such a way as if the device rotation is changed

9) Then the user returns back to the app

10) But the dialog box shown is no more being shown

11) But the async Task is running in the background and is still performing its task

12) The task is actually done but the user thinks that the task is still not done.

like image 894
Rahulrr2602 Avatar asked Dec 20 '18 10:12

Rahulrr2602


2 Answers

In your on_resume() method verify the screen size and the font size have not changed. If they have, adjust, remove and recreate, your dialog box appropriately. This is similar to handling orientation changes while suspended (adding font size as a condition).

EDIT:

Add android:configChanges="fontScale|density" to your manifest file.

static int fontScale = 1;
static int densityDpi = 0;

public void onConfigurationChanged(Configuration newConfig) {
  if (newConfig.fontScale != fontScale) {
    // destroy and recreate dialog adjusting for font size.
    fontScale = newConfig.fontScale;
  }
  if (newConfig.densityDpi != densityDpi) {
    // destroy and recreate dialog adjusting for new screen size.
    densityDpi = newConfig.densityDpi;
  }
}

It would probably be better to utilize notifications for the async task, since they are unaffected by config changes and are not terminated in low memory conditions.

like image 53
Strom Avatar answered Oct 07 '22 22:10

Strom


When you rotate the phone, your activity is actually destroyed and recreated. This includes your dialog. There are two ways to fix it:

1) Turn off this "helpful" functionality. If you don't have different layouts for landscape and portrait its what I'd suggest. Add android:configChange="orientation|resize" to your activity in the manifest.

2) Implement onSaveInstanceState/onRestoreInstanceState and have a variable that says whether or not you need to recreate and relaunch the dialog.

Suggestions: Try using WorkManager to schedule the task and also ViewModel from the android architecture components which helps to retain the state even after the configuration changes

like image 37
Swanand Keskar Avatar answered Oct 07 '22 20:10

Swanand Keskar