当应用程序从后台转到前台时显示 activity 时出现问题

Problems while showing activity when app comes from background to foreground

我创建了一个 android 应用程序。每次用户从后台转到前台时,我都试图调用 PinLoginActivity 。我正在使用以下方法来检测我的应用程序中所有活动的 onStartonStop,并且它工作得很好。

class YourApplication : Application() {

  override fun onCreate() {
    super.onCreate()
    registerActivityLifecycleCallbacks(AppLifecycleTracker())
  }

}

class AppLifecycleTracker : Application.ActivityLifecycleCallbacks  {
  private var numStarted = 0

  override fun onActivityStarted(activity: Activity?) {
    if (numStarted == 0) {      
       Intent intent = new Intent(getApplicationContext(), PinLoginActivity.class);
       intent.putExtra("LockScreen", true);
       startActivity(intent);
    }
    numStarted++
  }

  override fun onActivityStopped(activity: Activity?) {
    numStarted--
    if (numStarted == 0) {        
    }
  }   
}

现在的问题是:

我正在从我的应用程序中访问图库。一旦我打开画廊,select 图像并返回到我的应用程序。我收到 PinLoginActivity。在这种情况下,如何避免调用要调用的 PinLoginActivity

此示例介绍如何在应用程序中设置标志并检查图库是否即将打开

public class Application extends android.app.Application {
static boolean isGallery;
@Override
public void onCreate() {
    super.onCreate();
}

public static void setGalleryFlag(boolean isTrue){
    isGallery = isTrue;
}

public void checkGallery(){
    if (isGallery){
        //hide your activity
    }else{
        //operate normally
    }
}

}

所以当您要打开画廊时,只需将标志设置为 true

private void startGallery() {
    Application.setGalleryFlag(true);
    //open the gallery now
}