'How to close/exit outside settings intent created by the application

I'm currently working on a simple application that is used to force stop some applications with the help of Accessibility permission. It automatically opens the Application Info Settings page and presses the Force Stop button (The main part is working fine).

But After the process, I can't minimize or close the settings page. I used onAppStopped() method (starts the MainActivity intent), but it repeats the process recursively. I think it is because the settings page is not a part of the main application. Is there any way to override this issue?

MainActivity

protected void onCreate(Bundle savedInstanceState) {
        FSAccessibilityService.setClient(this);

        forceStopApp("package name");
    }

public void forceStopApp(String packageName) {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.parse("package:" + packageName);
        intent.setData(uri);
        startActivity(intent);
    }

@Override
    public void onAppStopped() {
        startActivity(new Intent(this, MainActivity.class);
    }

FSAccessibilityService extends AccessibilityService

@Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        performForceStop(event);
    }

private void performForceStop(AccessibilityEvent event) {
    AccessibilityNodeInfo source = event.getSource();

    performClickButtonByText(source, "FORCE STOP");
    performClickButtonByText(source, "OK");

    getClient().onAppStopped();
}

public interface FSClient {
        void onAppStopped();
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source