Android application changing from background to foreground

Course Queries Syllabus Queries 3 years ago

1.1K 2 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 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,

  1. 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.

  2. 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.

0 views
0 shares

profilepic.png
manpreet 3 years ago


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) {

}

}


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum