Ways To Bring An Android App In Background To Foreground
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
Solution 1:
In MyMainActivity definition (AndroidManifest.xml):
<intent-filter><actionandroid:name="intent.my.action" /><categoryandroid:name="android.intent.category.DEFAULT" /></intent-filter>Programmatically bringing application to foreground:
Intent it = newIntent("intent.my.action");
it.setComponent(newComponentName(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.
Solution 2:
Yes, what you are saying is correct, have a BroadcastReciever and fire an intent to your Activity to bring it to foreground. Word of caution however regarding the Activity life cycle.
Android OS can take your activity from onPause() to onStop() and onDestroy() depending on system resources. So in such a case, your calling Activity will restart again, so take precautions there. Else, very easy to run into NullPointerExceptions
Post a Comment for "Ways To Bring An Android App In Background To Foreground"