Force application to restart from first Activity (when a permission is denied)

后端 未结 5 1813
庸人自扰
庸人自扰 2021-02-02 09:46

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

相关标签:
5条回答
  • 2021-02-02 10:05

    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(...) {
        ...
      }
    }
    
    0 讨论(0)
  • 2021-02-02 10:07

    You can follow below steps for that.

    1. Create a Base Activity that extends AppcompatActivity for maintaining all permissions and check permissions onResume().
    2. Extend Base Activity from all activities.
    3. If any permission is denied, you can do whatever you want from Base Activity.

    Happy Coding! :)

    0 讨论(0)
  • 2021-02-02 10:17

    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.

    0 讨论(0)
  • 2021-02-02 10:19

    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.

    0 讨论(0)
  • 2021-02-02 10:19

    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.

    0 讨论(0)
提交回复
热议问题