handleWindowVisibility: no activity for token android.os.BinderProxy
Asked Answered
U

6

17

I have a Login screen and upon success login, it finishes and shows another page that has information about the user.
I read about this post and also this post.
I also read a lot about how we extend Application class but still it is not possible for me to run this code.
Below you can find my code and I will explain the error also.

This is how I user Volley to call an AsyncTask:
The error is like no activity for token android.os.BinderProxy and it will come when I call startActivity(intent);.
I know this error is because the activity is killed and the AsyncTask after Volley response wants to use a killed context but I don't know how to fix it.

Util.request_function(
     activity,
     MainActivity.user_session,
     key_value,
     new VolleyCallback() {
          @Override
          public void onSuccess(JSONObject result, Context context) {

                 Activity activity = 
                 MyBaseActivity.myCustomApplication.getCurrentActivity();
                 Intent intent = new Intent(activity, SelfieCapture.class);
                 startActivity(intent);
                 finish();
          }
          @Override
          public void onError(String result) {

          }
});

I have interfaces as below:
VolleyCallback.java:

public interface VolleyCallback {
    void onSuccess(JSONObject result) throws JSONException;
    void onError(String result) throws Exception;
}

Util.java

public static void request_function(Context context, CognitoUserSession cognitoUserSession, Map<String, String> key_value, final VolleyCallback callback) {
        JSONObject jsonBody = new JSONObject();
        CustomJSONObjectRequest postRequest = new CustomJSONObjectRequest(Request.Method.POST,
                MainActivity.API_URL,
                null,
                response -> {
                   JSONObject jsonObject = (JSONObject) response;
                   //SoMe Stuff//
                   callback.onSuccess(null);
             }, error -> {
                   //Log Error//
             }){
                  @Override
                  public String getBodyContentType() {
                         return "application/json; charset=utf-8";
                  }

                  @Override
                  public Map<String, String> getHeaders() {
                  final Map<String, String> headers = new HashMap<>();
                  headers.put("Content-Type", "application/json");
                         return headers;
                  } 

                  @Override
                  public byte[] getBody() {
                         return jsonBody.toString().getBytes();
                  }
        };
     // Request added to the RequestQueue
     VolleyController.getInstance(context).addToRequestQueue(postRequest);

MyCustomApplication.java

public class MyCustomApplication extends Application {

    private Activity mCurrentActivity = null;

    public void onCreate() {
        super.onCreate();

    }

    public Activity getCurrentActivity() {
        return mCurrentActivity;
    }

    public void setCurrentActivity(Activity mCurrentActivity) {
        this.mCurrentActivity = mCurrentActivity;
    }
}

MyBaseActivity.java

public class MyBaseActivity extends Activity {
    public static MyCustomApplication myCustomApplication;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myCustomApplication = (MyCustomApplication)this.getApplicationContext();
    }
    protected void onResume() {
        super.onResume();
        myCustomApplication.setCurrentActivity(this);
    }
    protected void onPause() {
        clearReferences();
        super.onPause();
    }
    protected void onDestroy() {
        clearReferences();
        super.onDestroy();
    }

    private void clearReferences(){
        Activity currActivity = myCustomApplication.getCurrentActivity();
        if (this.equals(currActivity))
            myCustomApplication.setCurrentActivity(null);
    }
}
Unanswerable answered 9/9, 2019 at 5:24 Comment(0)
A
6

This code Seems correct but my only suspicion is that when you want to open the new activity in startActivity(intent), the error occurs.
So check the next fired class named SelfieCapture.class to see whether it extends from MyBaseActivity also.
Also consider that when you want to get the currentActivity, if you put it in onCreate, you will get null. For more information please refer to Understand the Activity Lifecycle.

Aciculum answered 10/9, 2019 at 5:24 Comment(0)
M
10

In some cases, I've found that using onCreate with a persistentState parameter can cause the issue:

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
    }

Changing the onCreate to only use the savedInstanceState parameter fixes the issue:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
Malca answered 27/7, 2020 at 22:42 Comment(0)
A
6

This code Seems correct but my only suspicion is that when you want to open the new activity in startActivity(intent), the error occurs.
So check the next fired class named SelfieCapture.class to see whether it extends from MyBaseActivity also.
Also consider that when you want to get the currentActivity, if you put it in onCreate, you will get null. For more information please refer to Understand the Activity Lifecycle.

Aciculum answered 10/9, 2019 at 5:24 Comment(0)
B
2

I Don't know Why and How but what i did is I just build > Rebuilt Project then after it rebuilt, I just File > Invalidate Caches / Restart

and then my activity was working perfectly...!

Benjamin answered 29/1, 2021 at 7:47 Comment(0)
T
1

In my case, This occurred when i was trying to pass a really long JSON string through intent extras. From what I can infer , passing a really large value through the extra bundle might lead to memory issues when starting the new intent. Try using shared preferences or serializable objects to share large stuff between activities

Hope that helped!

Tombaugh answered 19/1, 2021 at 4:0 Comment(0)
S
0

I got this error in my Login activity because i had forgotten to turn the emulator internet on! I turn it on and it solved.

i mean this error : no activity for token android.os.BinderProxy

Sjoberg answered 25/4, 2022 at 0:48 Comment(0)
D
0

For my flutter application, firebase needed the SHA-1 fingerprint. I had added it previously but somehow it had been changed and i just added the new SHA-1 fingerprint of my application to the firebase android app then it worked. Hope it helps.

Dropforge answered 30/10, 2023 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.