Android application changing from background to foreground

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

I'm having an app A with Settings and Authentication activities. The Authentication activity is launched whenever some other app(say App B) is launched, so if the 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 activity of App A. If the authentication is successful then immediately I'm killing the Authentication Activity using finish(). But this will finish the Authentication activity and it will bring the 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 activity is launched and on successful authentication the App B's 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.

profilepic.png
manpreet 2 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.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community