0

On Android, I'm trying to design an activity that will walk users through some system settings change. I know how to detect if the setting is enabled or not and I'll put that check in a loop in a background thread and it correct detects when the user makes the change. I want my activity to automatically pop back up to the foreground when the change happens. I have a startActivity call there but it's not working.

final Thread mSettingCheck = new Thread() {
    @Override
    public void run() {
        try {
            for (int i = 0; i < 600; i++) {
                if (WizardUtils.isSettingChecked()) {
                    Handler handler = new Handler(Looper.getMainLooper());
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                        Intent intent = new Intent(getApplicationContext(), WizardActivity.class);
                        startActivity(intent);
                        }
                    }); 
                    Log.i("tag", "yay!");
                    break;
                }
                sleep(500);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};

I get the log output line saying "yay" so I know it's detecting, but I don't see the activity launch to the foreground. Anyone know what I'm doing wrong?

ajma
  • 12,106
  • 12
  • 71
  • 90
  • 1
    I don't understand, you are trying ro run `WizardActivity` from `WizardActivity` ?`new Intent(WizardActivity.this, WizardActivity.class);` right ? – Mickey Tin Jan 21 '14 at 20:14
  • I want the user to start on WizardActivity and it will have some instructions to go to system settings to check a box. In a background thread, I want my activity to poll for the setting change and when the setting is checked, I want to bring the user back to WizardActivity to move on to the next step. – ajma Jan 21 '14 at 20:15
  • I changed it to Intent(getApplicationContext(), WizardActivity.class) to make it more clear. – ajma Jan 21 '14 at 20:18
  • You want to bring user from "System settings" to your activity ? – Mickey Tin Jan 21 '14 at 20:18
  • Yes. I want to bring the user from system settings to my activity – ajma Jan 21 '14 at 20:18
  • Try this : http://stackoverflow.com/questions/12074980/bring-application-to-front-after-user-clicks-on-home-button – Mickey Tin Jan 21 '14 at 20:23
  • shouldn't you run your startActivity(intent) in runOnUiThread? – alecnash Jan 21 '14 at 23:36
  • That's what the MainLooper is. It's the UI thread. – ajma Jan 21 '14 at 23:50
  • you're right. did you try debugging the whole stuff? – alecnash Jan 21 '14 at 23:57
  • yes, my log message fires. I see it in logcat. – ajma Jan 22 '14 at 23:19

1 Answers1

0

The fix here was to use a Pending Intent.

private void returnToWizard() {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent("android.intent.category.LAUNCHER");
            intent.setClassName("package name", "activity name");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            PendingIntent i2 = PendingIntent.getActivity(getApplicationContext(), 0, intent,
                    Intent.FLAG_ACTIVITY_NEW_TASK);
            try {
                i2.send(getApplicationContext(), 0, intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
ajma
  • 12,106
  • 12
  • 71
  • 90