Android中Tapresearch SDK的onSurveyModalClosed监听失效,如何检测SurveyActivity是否结束?
Got it, let's figure out how to detect when TapResearch's SurveyActivity finishes since their built-in onSurveyModalClosed() listener isn't working for you. Here are a few solid approaches that don't rely on that broken callback:
This method works regardless of how TapResearch launches the SurveyActivity—it listens for every activity's lifecycle events across your app.
Implementation Steps:
- Add a lifecycle callback to your app's
Applicationclass (or your main activity, if you prefer):
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { // Leave unused lifecycle methods empty @Override public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {} @Override public void onActivityStarted(@NonNull Activity activity) {} @Override public void onActivityResumed(@NonNull Activity activity) {} @Override public void onActivityPaused(@NonNull Activity activity) {} @Override public void onActivityStopped(@NonNull Activity activity) {} @Override public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {} @Override public void onActivityDestroyed(@NonNull Activity activity) { // Check if the destroyed activity is TapResearch's SurveyActivity if ("com.tapr.internal.activities.survey.SurveyActivity".equals(activity.getClass().getName())) { // Run your app-resume logic here resumeAppFunctionality(); } } }); } private void resumeAppFunctionality() { // Example: Resume a paused game, refresh UI state, or re-enable user interactions // If you need to trigger this in a specific activity, use LiveData or LocalBroadcastManager to send a signal } }
- Don't forget to declare your custom
Applicationclass in theAndroidManifest.xml:
<application android:name=".MyApp" <!-- other attributes --> >
Pro Tip:
If you need to trigger resume logic in a specific activity (not the application class), use a LiveData object or LocalBroadcastManager to send a notification that the survey has closed.
If TapResearch's SDK lets you retrieve the Intent used to launch SurveyActivity, you can use Android's modern Activity Result API to listen for when it finishes.
Implementation Steps:
public class MainActivity extends AppCompatActivity { private ActivityResultLauncher<Intent> surveyLauncher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize the launcher surveyLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { // This fires when SurveyActivity finishes (regardless of result code) resumeApp(); } ); // When you're ready to launch the survey (hypothetical SDK method) Intent surveyIntent = TapResearch.getSurveyLaunchIntent(this); surveyLauncher.launch(surveyIntent); } private void resumeApp() { // Your resume logic here } }
Note:
This only works if you can explicitly launch the survey via an intent from your code. If TapResearch's SDK launches the activity internally (e.g., via a service or broadcast), this method won't work.
This method requires user authorization to enable accessibility features, so it's not ideal for most apps—but it's a fallback if the above options fail.
Implementation Steps:
- Create an Accessibility Service class:
public class SurveyCloseDetectorService extends AccessibilityService { @Override public void onAccessibilityEvent(AccessibilityEvent event) { if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { String packageName = event.getPackageName().toString(); String className = event.getClassName().toString(); // Check if the SurveyActivity window is closing if ("com.tapresearch.sdk".equals(packageName) && "com.tapr.internal.activities.survey.SurveyActivity".equals(className) && !event.isEnabled()) { // Send a broadcast to notify your app the survey closed Intent closeIntent = new Intent("SURVEY_CLOSED_ACTION"); sendBroadcast(closeIntent); } } } @Override public void onInterrupt() {} }
- Register the service in your
AndroidManifest.xml:
<service android:name=".SurveyCloseDetectorService" android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"> <intent-filter> <action android:name="android.accessibilityservice.AccessibilityService" /> </intent-filter> <meta-data android:name="android.accessibilityservice" android:resource="@xml/accessibility_config" /> </service>
- Create
res/xml/accessibility_config.xml:
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:accessibilityEventTypes="typeWindowStateChanged" android:accessibilityFeedbackType="feedbackGeneric" android:accessibilityFlags="flagDefault" android:canRetrieveWindowContent="true" android:description="@string/accessibility_service_desc" />
- In your activity, register a broadcast receiver to listen for the survey close event.
内容的提问来源于stack exchange,提问作者Fat-Bee

