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

2019-01-17 16:53发布

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..

7条回答
Deceive 欺骗
2楼-- · 2019-01-17 17:32
    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;
}
查看更多
Lonely孤独者°
3楼-- · 2019-01-17 17:35

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);
}
查看更多
Rolldiameter
4楼-- · 2019-01-17 17:40

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

查看更多
We Are One
5楼-- · 2019-01-17 17:46

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

查看更多
我只想做你的唯一
6楼-- · 2019-01-17 17:48

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();
}
查看更多
beautiful°
7楼-- · 2019-01-17 17:53

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.

查看更多
登录 后发表回答