Here is the scenario:
AndroidManifest.xml defines a single Activity with android:launchMode="singleTask"
. (This means there should be a single activity in the stack throughout the entire application lifecycle, right ?)
During Activity.onCreate()
, a broadcast receiver is programmatically created and listens for incomming SMS. The receiver remains active even after Activity.onPause()
by design.
When the user is done with the application, he presses the device Home button which calls Activity.onPause()
and the application disappears. The device shows then the Android home screen.
Upon receiving SMS, the broadcast receivers receives SMS and tries to show up the Activity via:
Intent it = new Intent(context, Akami.class);
it.setAction(Intent.ACTION_MAIN);
it.addCategory(Intent.CATEGORY_LAUNCHER);
it.setComponent(new ComponentName(context.getPackageName(), "MyActivity"));
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(it);
However, the activity is NOT showed up to the user.
Use moveTaskToBack() to move your app to the background. After Home button is pressed, it takes 5 seconds to bring the app back to foreground.
An app is running in the background when both the following conditions are satisfied: None of the app's activities are currently visible to the user. The app isn't running any foreground services that started while an activity from the app was visible to the user.
In MyMainActivity
definition (AndroidManifest.xml):
<intent-filter>
<action android:name="intent.my.action" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Programmatically bringing application to foreground:
Intent it = new Intent("intent.my.action");
it.setComponent(new ComponentName(context.getPackageName(), MyMainActivity.class.getName()));
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.getApplicationContext().startActivity(it);
Note: context.startActivity(it)
would NOT work when the context
object is same as the activity one wants to bring up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With