It is known that when we deny permissions at runtime in Android 6.0 and resume the app from the recent menu, the app process
You should put code to check the permission status in your onResume
callback. If the user is switching to the System settings permission activity, your Activity will get paused. If the user enables a permission and then returns to your Activity, then onResume
will be called, and you will have an opportunity to check for the new permission at that point. Then, you can do whatever you need to restart your activity, such as calling startActivity
with an Intent that has FLAG_ACTIVITY_NEW_TASK
and FLAG_ACTIVITY_CLEAR_TASK
and launches your Activity again.
override fun onResume() {
super.onResume()
// check for permission
checkPermissionsAndRestartIfNecessary()
}
private fun checkPermissionsAndRestartIfNecessary() {
if (ContextCompat.checkSelfPermission(...) {
...
}
}
You can follow below steps for that.
Happy Coding! :)
You should be handling Activity
(re)creation using the Bundle
provided by onCreate
, saved by onSaveInstanceState
, more information here.
Pass in the Bundle
all you need in order to recover from a previous state and restore your UI seamlessly. Your Activity
can be recreated by many reasons, cancellation of permissions is just one of them, screen rotation is another, so as long as you can survive one, you can survive all of them.
Did u mean this?
MainActivity:
public class MainActivity extends AppCompatActivity {
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView)findViewById(R.id.txt);
txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ActivityB.class);
intent.putExtra("from","MainActivity");
startActivity(intent);
finish();
}
});
}
}
ActivityB :
public class ActivityB extends AppCompatActivity {
String intentTxt="";
final int MY_PERMISSIONS_REQUEST_PHONE_CALL = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
intentTxt = getIntent().getStringExtra("from");
}
@Override
protected void onResume() {
super.onResume();
if(intentTxt.equals("MainActivity")) {
if (ContextCompat.checkSelfPermission(ActivityB.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(ActivityB.this, new String[]{android.Manifest.permission.CALL_PHONE},
MY_PERMISSIONS_REQUEST_PHONE_CALL);
}
}
intentTxt = "";
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_PHONE_CALL: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "32434"));
startActivity(intent);
} else {
Intent intent = new Intent(ActivityB.this,MainActivity.class);
startActivity(intent);
finish();
}
return;
}
}
}
}
You can start the splashActivity/HomeActivity from onRequestPermissionsResult of ActivityB.Check for permission in onResume of ActivityB so that the permission dialog appears when the ActivityB is called for the first time and also when resumed.
I don't know if anyone is still looking for an answer on this, but I'd solve by reversing the logic, instead of waiting for a flag indicating the restart/recreate due to the denial of permission, pass a flag indicating that the Activity was started by another Activity and not recreated by the system. So if FLAG doesn't come in extras then the activities stack must be destroyed and first activity lauched.
Hope it helps someone.