0

I have the following code in my kiosk app:

class MyActivity extends AppCompatActivity
{
...

@Override
  protected void onPause()
  {
    super.onPause();
    Log.d("DEBUG", "Pause");
    Intent intent = new Intent(MyActivity.this, MyActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(intent);
  }
}

When I press Home button, I get the log, and...nothing happens.

What should I do to make my Activity appear again?

Nestor
  • 8,194
  • 7
  • 77
  • 156

2 Answers2

1

onPause() is generally not a good place to start new Activities. Your Activity is generally being paused because another application is launching, the user turned off the screen, or hit the home button.

Regardless of which of these is happening, that thing is going to continue happening. The screen will still be off, the other Activity will still be launched, or the user's home screen will still be shown.

I would re-evaluate what you are doing and your strategy for doing it.

Edit: Since you are developing a kiosk app, the path forward is a little different. Your method still will not work, so here are some alternatives.

As of API 21 you should use startLockTask() to lock the user into the current task. Whenever possible, you should require that the kiosk device run API 21 or above so you can leverage this feature.

In older versions of Android, you will want to tell the OS that your app is a "launcher" by adding the HOME category to your Activity's intent filter in the manifest. This will relaunch your app when the user tries to navigate home. You will also want to configure the device to use your app as the "launcher" by default to lock the user in. See this StackOverflow post for more detail.

Community
  • 1
  • 1
Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • Then what would you use to restart the Activity and make it appear again? – Nestor Oct 07 '15 at 18:38
  • In `onPause()`? I woudn't. The user is probably leaving the app for a reason, and trying to stop them certainly won't make them want to keep using my app. – Bryan Herbst Oct 07 '15 at 18:40
  • I _intend_ to stop the user. It is a kiosk app. So, any idea how to make the activity to appear again? – Nestor Oct 07 '15 at 18:49
  • Ah, that is a crucial piece of information that should have been in your original post. I have updated my answer with more information on how to implement a kiosk app. – Bryan Herbst Oct 07 '15 at 19:54
  • It is an API level 16 application. I added `intent.addCategory(Intent.CATEGORY_LAUNCHER);` when I try to relaunch it. Why doesn't it have any effect? Should I declare it in the manifest file – Nestor Oct 08 '15 at 05:24
  • By adding the category to the intent you use to start the activity, you are telling the OS to find an Activity that already has that category in its manifest. As mentioned in the link I posted and in my post, you need to set the category in your manifest. – Bryan Herbst Oct 08 '15 at 13:25
0

Well, using the normal Intent approach is not working.

Fortunately there is a workaround to make the Activity appear again. Use a PendingIntent like this:

Intent intent = new Intent(getBaseContext(), MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Intent mStartActivity = new Intent(this, MyActivity.class);
int mPendingIntentId = 789;
PendingIntent mPendingIntent = PendingIntent.getActivity(this, mPendingIntentId, mStartActivity, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), mPendingIntent);

The only drawback, that the AlarmManager sets off after 5 seconds, no matter I set it with currentTimeMillis(). It is a known limitation of the OS.

Nestor
  • 8,194
  • 7
  • 77
  • 156