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

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 is killed and the app is forcibly restarted. This is allegedly in order to prevent any security snafus:

enter image description here

  • Why does ContentResolver.requestSync not trigger a sync?
  • Genymotion, “Unable to load VirtualBox engine.” on Mavericks. VBox is setup correctly
  • Data not being saved to Firebase from Android App
  • Unable to detect application ABI's when trying to debug NDK
  • How to capture the “virtual keyboard show/hide” event in Android?
  • Small Caps on TextViews, EditTexts, and Buttons in Android
  • It is remarkable that, when resumed, the app restarts from the last Activity we left it on. The OS apparently keeps track of the last Activity the user visited.

    Unfortunately, this is a problem, as it breaks the flow of the business logic. The user cannot be allowed to access the app midway in this manner.

    My question is, is there a way to force the app to restart from the first Activity of the application, instead of the one the user left it on?

    Are there any callbacks associated with an application restart / process kill / permissions toggle?

    Is this a wrong-headed approach? If so, how? And what would be the correct approach?

    This behaviour has of course been observed before:

    1. Android Preview M: activity recreates after permission grant

    2. Application is getting killed after enable/disable permissions from settings on Android M nexus 6

    3. Android Marshmallow: permissions change for running app

    4. All the permissions of my app are revoked after pressing “Reset app preferences”

    5. Android 6 permission – Crashes when toggling permission in Setting and go back to app

    Related posts:

    Unable to execute dex: Multiple dex files define
    BitmapFactory.decodeResource() returns null for shape defined in xml drawable
    Android pop fragment from backstack with animation
    Where to add folder of classes in Android Studio?
    Why does AsyncTask run in the main Thread of an application?
    Android Shared Preferences across different applications
  • What's the difference between apklib and jar files?
  • Starting Activity from Fragment causes NullPointerException
  • how to make Multiplayer Game
  • How to tell ProGuard to keep private fields without specifying each field
  • How to Apply the Textchange event on EditText
  • Change phone number in emulator - Android development
  • One Solution collect form web for “Force application to restart from first Activity (when a permission is denied)”

    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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.