Start action when returning from Android settings screen
My script:
- I open an activity
- I'm doing a check (internet check)
- In case of a false check, launching a warning dialog
- Now I go to the settings to go back to the Internet by clicking the Back button
- The dialogue is not rejected, it is still on the screen.
- My goal is to restart the activity when I return from the settings screen
CODE {Edited}
public void open(){
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage(getApplicationContext().getResources().getString(R.string.searchFilterLocationMessage));
alertDialogBuilder.setPositiveButton(R.string.Ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
/*Intent intent = new Intent(Settings.ACTION_SETTINGS) ;
this.startActivity(intent);
*/
startActivityForResult(new Intent(Settings.ACTION_SETTINGS), 0);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
}//onActivityResult
source to share
Run customization setup:
startActivity(new Intent(Settings.ACTION_SETTINGS));
and select the current activity in the onResume method:
public void onResume(){
super.onResume();
//Do your work
}
after setting screen support your onResume method will call and here you can get your location.
source to share
Starting Activity
with run mode singleTask
by calling startActivityForResult(intent, requestCode)
immediately returns the cancellation result. You can see in the debugger what onActivityResult()
is called even before the system settings start Activity
.
As a quick workaround, I suggest using a flag indicating whether the settings were called Activity
or not. how
-
flag setting
private boolean flag = false;
-
using
startActivity()
isteadstartActivityForResult()
@Override public void onClick(DialogInterface arg0, int arg1) { startActivity(new Intent(Settings.ACTION_SETTINGS)); flag = true; }
-
checking the flag in
onResume()
@Override protected void onResume(){ super.onResume(); if (flag) { startActivity(new Intent(this, MainActivity.class)); finish(); } }
source to share