How to know "Don't keep activities" is enabled in ICS?
Asked Answered
C

7

14

How to know in settings "Don't keep activities" option is enabled in ics via code. If it is in enabled,how to prevent the activity.

Note: I have used more activities in my application.Because of that settings if I move one activity to another the first activity is destroyed.I can save my data on the onSaveInstanceState. With out using saved instance state is there any way to do...

Thanks in advance..

Cairn answered 25/7, 2012 at 12:40 Comment(0)
B
14

That flag is there for exactly this reason. The idea is to check and make sure your app handles its activities being destroyed. You can't override it because in the real world it might happen even without that flag if the device is low on memory. You need to code things to handle this case not override it.

Brann answered 25/7, 2012 at 12:57 Comment(6)
Is there a way to find that "Don't keep activities" settings via code...?Cairn
Not that I know of and again - Why would you want to? You should NEVER use that flag as a reason to do anything.Brann
I have a simple case : An app which have a Preference activity. Clicking on option button close immediately preferences when this cell parameter is set... It's usefull to know to beware usersZabaglione
I would disagree here and the developers should be able to check this setting and prompt the user appropriately that the app can misbehave with that setting ON. Even if the developer writes a perfect app which survives activity restarts, this setting causes a problem in scenarios where your Activity launches another Activity and the other activity belongs to another process (e.g gallery), even with startActivityForResults Android will close our Activity and we wont ever get the Activity result from the Gallery Activity.Bowerman
@KevinKrumwiede tryout the test case I mentioned and then make claims about people being "incompetent". Its better for a developer to know the limitations of the system under which his app wont work rather than let the users remain clueless about why something is not working.Bowerman
@NileshPawar I did. "Don't keep activities" has no effect whatsoever on receiving results from activities in other processes. The test case you mentioned was most likely the result of the gallery starting in another task, and you never receive results from an activity in another task. I stand by what I said: apps should work perfectly with "don't keep activities" on (that's the whole point of it) and advertising that they don't only advertises the incompetence of the developer.Oligochaete
W
22

import the following package

import android.provider.Settings;

Then you can check if the option is enable using this code:

Settings.System.getInt(getContentResolver(),Settings.System.ALWAYS_FINISH_ACTIVITIES, 0);

and you can change it using this:

Settings.System.putInt(getContentResolver(),Settings.System.ALWAYS_FINISH_ACTIVITIES,  0);

Hope it helps

Wig answered 19/10, 2012 at 14:54 Comment(3)
I am getting warnings :Setting always_finish_activities has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value. and Setting always_finish_activities has moved from android.provider.Settings.System to android.provider.Settings.Global, value is unchanged.Aftereffect
please help me , to disable ALWAYS_FINISH_ACTIVITIESAftereffect
it doesn't take effect when you try to disable it, putInt() and just like you said. But the reading returns correct values for when it's on/off. Any idea why? WRITE_SETTINGS permission is in the manifest tooDube
I
16
    Settings.System.ALWAYS_FINISH_ACTIVITIES 

now is deprecated so you should use

    Settings.Global.ALWAYS_FINISH_ACTIVITIES.equals("1")

to check if this option is enabled

update(thanks Aviv Ben Shabat comment):

to handle all sdk versions I've created a method:

  public static boolean isFinishActivitiesOptionEnabled(Context context) {
    int result;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        result = Settings.System.getInt(context.getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0);
    } else {
        result = Settings.Global.getInt(context.getContentResolver(), Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0);
    }

    return result == 1;
}
Infer answered 15/3, 2013 at 9:24 Comment(2)
I think the 2nd line should also be "Settings.SYSTEM.getInt"Ostend
Note, that you cannot change Global Settings (unlike System)Topography
B
14

That flag is there for exactly this reason. The idea is to check and make sure your app handles its activities being destroyed. You can't override it because in the real world it might happen even without that flag if the device is low on memory. You need to code things to handle this case not override it.

Brann answered 25/7, 2012 at 12:57 Comment(6)
Is there a way to find that "Don't keep activities" settings via code...?Cairn
Not that I know of and again - Why would you want to? You should NEVER use that flag as a reason to do anything.Brann
I have a simple case : An app which have a Preference activity. Clicking on option button close immediately preferences when this cell parameter is set... It's usefull to know to beware usersZabaglione
I would disagree here and the developers should be able to check this setting and prompt the user appropriately that the app can misbehave with that setting ON. Even if the developer writes a perfect app which survives activity restarts, this setting causes a problem in scenarios where your Activity launches another Activity and the other activity belongs to another process (e.g gallery), even with startActivityForResults Android will close our Activity and we wont ever get the Activity result from the Gallery Activity.Bowerman
@KevinKrumwiede tryout the test case I mentioned and then make claims about people being "incompetent". Its better for a developer to know the limitations of the system under which his app wont work rather than let the users remain clueless about why something is not working.Bowerman
@NileshPawar I did. "Don't keep activities" has no effect whatsoever on receiving results from activities in other processes. The test case you mentioned was most likely the result of the gallery starting in another task, and you never receive results from an activity in another task. I stand by what I said: apps should work perfectly with "don't keep activities" on (that's the whole point of it) and advertising that they don't only advertises the incompetence of the developer.Oligochaete
L
10

I agree with @Kaediil that android applications must work well while "Don't keep activities" option is checked.

Bu for some reason if you have to check "alwaysFinishActivities" value you can use code below;

/**
 * returns true if AlwaysFinishActivities option is enabled/checked
 */
private boolean isAlwaysFinishActivitiesOptionEnabled() {
    int alwaysFinishActivitiesInt = 0;
    if (Build.VERSION.SDK_INT >= 17) {
        alwaysFinishActivitiesInt = Settings.System.getInt(getApplicationContext().getContentResolver(), Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0);
    } else {
        alwaysFinishActivitiesInt = Settings.System.getInt(getApplicationContext().getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0);
    }

    if (alwaysFinishActivitiesInt == 1) {
        return true;
    } else {
        return false;
    }
}

If alwaysFinishActivities option is checked and you want it to be unchecked;

You can direct user to "Settings -> Developer options" to uncheck that value. (This is better than to get extra scary permissions and set this value programatically)

/**
 * shows Settings -> Developer options screen
 */
private void showDeveloperOptionsScreen(){
    Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivity(intent);
}
Lanceted answered 12/4, 2013 at 7:11 Comment(0)
W
7

The questioner has right and he has got a good question, which is still not be answered :-)

I tested this issue with a clean simple example. The fact is, that when the option 'do not keep activities' is checked, then activities will be killed. For example: A Mainactivity calls a subactivity. The mainactivity is killed. Pressing back on the subactivity results in restarting the Mainactivity. This can be a problem, when the mainactivity is doing longrunning stuffs like loading content.

My conclusion on this. The option 'do not keep activites' is an developer option and should not be set under normal user-conditions. So developer should be able to detect this setting.

Whipperin answered 10/8, 2012 at 2:33 Comment(4)
Your are missing the point. The option might not be set, but on a phone that has low memory, the effect may be exactly the same. The OS will get rid of the activities immediately to reclaim memory, so you may have exactly the same thing happening even if the flag is not set.Brann
Agreed with @Kaediil. Regardless of if it's on, the same behavior can happen based on low memory conditions or various other scenarios. Your app should behave correctly with it on.Gignac
Even phones with low memory will not be as forceful as keeping this option checked. Take a Facebook login for example, it is launched in a separate activity and will never work with "Don't keep activities" checked, however, it works on all phones.Syncope
It should not be set under normal conditions, but the only consequence of it being set should be that apps may use a little more CPU and battery because they're constantly recreating activities. From the user's perspective, apps should function perfectly normally with this option turned on. If they don't, the developer has done something wrong.Oligochaete
A
1

int value = Settings.System.getInt(getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0);

Alenaalene answered 26/9, 2013 at 7:1 Comment(0)
P
1

You could also use an intent and kill the activity each time. Just call ReturnToMain in your onBackPressed(), and it should defeat the "Don't keep activities" problem. This has been one of the problems I have been facing overseas with foreign handsets.

public void ReturnToMain() {
    Intent view_user = new Intent(PanicAddUpdate.this, PanicAddMain.class);
    view_user.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(view_user);
    finish();
}

The other option would be @aegean's solution below. You will have to direct the user to "Settings -> Developer options" to uncheck that value. Here is what I used with a dialog box.Make sure to add the finish() so that the app doesn't crash when they push the back button, or try to restart the app after pressing the home button.

public void showDeveloperOptionsScreen(){

    new AlertDialog.Builder(this)
    .setTitle("Developer Options Detected!")
    .setMessage("In order for Sender to work properly, please uncheck the \"Don't keep activities\" option.")
    .setNegativeButton(android.R.string.no, null)
    .setPositiveButton(android.R.string.yes, new OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1) {
            Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            startActivity(intent);
            finish();
        }
    }).create().show();
}
Phrase answered 15/2, 2014 at 0:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.