1

below is the flow of the problem

  • user tries to make some in-app purchases.
  • the app launches google billing client activity which waits for users' payment responses.
  • User minimizes my app and goes to third party payment apps ( in India- UPI apps ) .
  • app is getting killed due to memory crunch.
  • user tries to resume the app, it is actually recreated instead of proper resuming as it was killed.
  • purchase is lost on play console status shown is "payment pending"
  • status changes "user canceled" in half an hour.

here I can not save any context before launching of google billing client activity is launched by google billing client payment flow, not by my code. google billing client's listener works only if has the same context, not on activity recreation.

Dinesh Sen
  • 11
  • 1

1 Answers1

0

Running a foreground service should help you.

Here is a sample foreground service class:

public class YourService extends Service {

    public static final String CHANNEL_ID = "YourForegroundServiceChannel";

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        createNotificationChannel();

        Intent notificationIntent = new Intent(this, MainActivity.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Your service name")
                .setContentText("Your service info")
                .setSmallIcon(R.drawable.ic_yours)
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1, notification);

        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void createNotificationChannel() {

        NotificationChannel serviceChannel = new NotificationChannel(
                CHANNEL_ID,
                "Your Foreground Service Channel",
                NotificationManager.IMPORTANCE_DEFAULT
        );

        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.createNotificationChannel(serviceChannel);
    }
}

Declare your service in AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

    <application ...>
        <service
        android:name=".service.YourService"
        android:enabled="true"
        android:exported="true"></service>
    </application>
</manifest>

Then start the service as follows:

public void startForegroundService() {
    Intent serviceIntent = new Intent(this.getActivity(), YourService.class);
    ContextCompat.startForegroundService(this.getActivity(), serviceIntent);
}

For more infos: https://developer.android.com/guide/components/foreground-services

matdev
  • 4,115
  • 6
  • 35
  • 56