可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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..
回答1:
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.
回答2:
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
回答3:
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;
}
回答4:
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);
}
回答5:
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.
回答6:
int value = Settings.System.getInt(getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0);
回答7:
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();
}