1

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.

6
  • How about your Manifest file? Did you register LogInPage in Manifest Commented Sep 4, 2015 at 3:36
  • What's the type and supertype of the class in which this piece of code exists ? Commented Sep 4, 2015 at 3:42
  • log ur stat and see value, Commented Sep 4, 2015 at 3:45
  • Set breakpoints and try running apps in debugging mode to see whats happening. Commented Sep 4, 2015 at 3:48
  • It may be because you're not using the right context. Application context should not be used to start a new activity. If you have no choice, you must then start a new task. Cf possiblemobile.com/2013/06/context -> Context Capabilities Commented Sep 4, 2015 at 4:04

1 Answer 1

2

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); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.