Bend
Bend

Reputation: 52

Get Current Context of Android App

I'm having issues getting the current context of my app. I have an object, that I reference/use in multiple places. When used anywhere in the app, it could return a KEY_STATUS_FAILED if the user's credentials have expired. In that case, I would like to, from which ever page the user is currently at, re-direct them to the login screen to re-establish credentials. I have tried several functions to get the base context with no success (currently using getApplicationContext() ). All help is appreciated!

//In common execution thread, after reading return from HTTP Post call
int stat = Integer.parseInt(checker.getString("status"));

if(stat == KEY_STATUS_FAILED){
    Log.d("Process t", "Key Status Failed");
    Intent i = new Intent(getApplicationContext(), LogInPage.class);
    startActivity(i); //this never occurs
}

I am currently getting the log output that it has entered the Key Status Failed if-loop. However, my application is not re-directing to LogInPage.class. I'm sure this is something basic, and I'm just committing a boneheaded mistake, but I'm pretty new to this, so thank you all for the help.

Upvotes: 1

Views: 1567

Answers (1)

Faiz Siddiqui
Faiz Siddiqui

Reputation: 2663

You are providing Application Context which is not responsible for starting an activity. You need to use Activity Context to do so.

Try using :

Intent i = new Intent(this, LogInPage.class);
startActivity(i);

or Intent i = new Intent(getActivity(), LogInPage.class);

or try creating a global context variable in your activity class and use that or try this :

Intent i = new Intent(((YourActivity) getActivity()), LogInPage.class);

Upvotes: 2

Related Questions