0

I am developing a paid app, which checks for the license upon starting (from onCreate() method) the app. After receiving the response codes from server, the subsequent callback methods allow(), dontAllow(), applicationError() executes which contains different Toasts.

Now my question is that whenever any Toasts gets generated and if suddenly I press device's back Button before the finishing off the Toast then the Toast gets stuck on the screen. It gets finished off only when I remove the app from task list.

 private class XYZLicenseCheckerCallback implements LicenseCheckerCallback {

    @Override
    public void allow(int reason) {
        if(isFinishing()) {
            return;
        }

        Toast.makeText(XyzActivity.this,"App purchase verified",Toast.LENGTH_SHORT).show();
        System.out.println("allowed called"+reason);
    }

    @Override
    public void dontAllow(int reason) {
        if(isFinishing()) {
            return;
        }

        displayResult(String.valueOf(reason));
        stopService(serviceIntent);
        Toast.makeText(XyzActivity.this,"App not licensed",Toast.LENGTH_SHORT).show();
        System.out.println("don't allow called"+reason);
    }

    @Override
    public void applicationError(int errorCode) {
        if(isFinishing()) {
            return;
        }

        Toast.makeText(XyzActivity2108.this,"Not able to verify the license,check your internet connection",Toast.LENGTH_SHORT).show();
        // finish();
        System.out.println("app error called"+errorCode);
    }
}

Here is my onBackpressed() method

@Override
public void onBackPressed() {
    finish();
}

Any help from your side please?

3 Answers3

0

Create a global variable Toast toast;

To show it use

toast = Toast.makeText(getContext(), "Hello World", Toast.LENGTH_SHORT);
toast.show();

Then on back press cancel the toast.

@Override
public void onBackPressed() {
    toast.cancel();
    finish();
}
Sp4Rx
  • 1,498
  • 3
  • 20
  • 37
0

Why not use snackbar instead?
This is your app message, not a system message.

Your toast remains on screen even when the activity is finished. Snackbar doesn't.

Refer to this answer from stackoverflow for more details.


Even if you don't want to use snackbar. This doesn't seem an expected behaviour from your code. What might be causing the problem is the long running thread that is still connected with the server, or has not finished yet. Check that the code triggering this(XYZLicenseCheckerCallback) is executing properly and is not making the app not responding.

Note: You must take extra considerations while interacting with the UI thread.

Pradeep Singh
  • 432
  • 5
  • 11
0

pass context in constructor and Toast with this context

 private class XYZLicenseCheckerCallback implements LicenseCheckerCallback {
 Activity mActivity;
 public XYZLicenseCheckerCallback (Activity mActivity){
 this.mActivity= mActivity;
 }
@Override
public void allow(int reason) {
    if(isFinishing()) {
        return;
    }

    Toast.makeText(mActivity,"App purchase verified",Toast.LENGTH_SHORT).show();
    System.out.println("allowed called"+reason);
}

@Override
public void dontAllow(int reason) {
    if(isFinishing()) {
        return;
    }

    displayResult(String.valueOf(reason));
    stopService(serviceIntent);
    Toast.makeText(mActivity,"App not licensed",Toast.LENGTH_SHORT).show();
    System.out.println("don't allow called"+reason);
}

@Override
public void applicationError(int errorCode) {
    if(isFinishing()) {
        return;
    }

    Toast.makeText(mActivity,"Not able to verify the license,check your internet connection",Toast.LENGTH_SHORT).show();
    // finish();
    System.out.println("app error called"+errorCode);
 }
 }