简体   繁体   中英

Start intent from package which cannot extends Activity

I have some problems handling http response codes. The problem is that the app crashes because I do not give a specific function to run. In this case I want to send the user back to login screen when the Httresponsecode is 401 otherwise the user can still use the app.

In my current code I have the following:

public boolean isUnauthorized(JSONObject response){
    try {
        if(response.getInt("StatusCode") == 401) {
            return true;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return false;
}

What I want is here is to call so that when calling this function app wide it will do the same on every screen.

Intent i = new Intent(Register.class, Register.activity);
startactivity(i);

However this isn't possible because the ResponseHandler cannot extends Activity. If I do this I get a stack-trace error containing looper.prepare() must be called.

Can you anybody tell me how I can call a new intent from here. The class containing above is in a folder called components and my app activities are in another folder in case it is needed for giving the right answer.

Intent i = new Intent(Register.class, Register.activity);
startactivity(i);

This should be

Intent i = new Intent(CurrentClass.this, Activity.class);
startactivity(i);

if you have fragment

Intent i = new Intent(getActivity(), Activity.class);
getActivity().startactivity(i);

You need to pass the current Activity or the Application Context as argument for your Intent.

If it is inside a fragment then

getActivity().startActivity(getActivity(), newActivity.class);

If it is inside a class then:

context.startActivity(context, newActivity.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM