I was also searching for some thing like this. But i did find some solutions which were out of syllabus for me like using background service or using getTasks method or using package manager like so on.
The simplest solution which i made was some thing like this.
public class MainApplication extends Application implements Application.ActivityLifecycleCallbacks {
private static boolean background = false;
private static int count = 0;
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(this);
}
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
}
@Override
public void onActivityStarted(Activity activity) {
if(background){
background = false;
Log.v("activityFocus", "Activity came in foreground ");
Toast.makeText(getApplicationContext(), "Foreground", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onActivityResumed(Activity activity) {
count++;
}
@Override
public void onActivityPaused(Activity activity) {
count--;
}
@Override
public void onActivityStopped(Activity activity) {
if(count==0){
Log.v("activityFocus", "Activity is in background ");
Toast.makeText(getApplicationContext(), "Background", Toast.LENGTH_SHORT).show();
background=true;
}
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
}
manpreet
Best Answer
2 years ago
I'm having an app A with Settings and Authentication activities. The Authentication b.com/tag/activity">activity is launched whenever some other app(say App B) is launched, so if the b.com/tag/authentication">authentication is successful then app B's launcher Activity is shown(a kind of app lock protection). I have two behaviors to explain here,
When I'm having my App A minimized, I will launch App B. This will trigger the Authentication b.com/tag/activity">activity of App A. If the b.com/tag/authentication">authentication is successful then immediately I'm killing the Authentication Activity using finish(). But this will finish the Authentication b.com/tag/activity">activity and it will bring the b.com/tag/activity">activity of App A that was minimized previously to front(I don't want this to happen) instead of taking it to new app.
If the App A is not running at all(not even minimized) than the Authentication b.com/tag/activity">activity is launched and on successful b.com/tag/authentication">authentication the App B's b.com/tag/activity">activity is shown.
Can someone explain me why these 2 different behaviors are being shown. Also in case 1 also I want to show the Activity of app 2 after successful Authentication.