How to know “Don't keep activities” is enabled in ICS?

时光怂恿深爱的人放手 提交于 2019-11-27 20:45:54

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.

Marin Vasvari

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

    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;
}

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);
}

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.

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

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!