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

后端 未结 7 703
遥遥无期
遥遥无期 2020-12-05 11:32

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 activit

相关标签:
7条回答
  • 2020-12-05 11:53

    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.

    0 讨论(0)
  • 2020-12-05 11: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.

    0 讨论(0)
  • 2020-12-05 11:59

    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

    0 讨论(0)
  • 2020-12-05 11:59

    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);
    }
    
    0 讨论(0)
  • 2020-12-05 12:01

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

    0 讨论(0)
  • 2020-12-05 12:11

    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();
    }
    
    0 讨论(0)
提交回复
热议问题