1

The more I research this, the more confused I get. I admit I'm pretty new to this Android stuff. Maybe someone can explain this to me in the context of my specific issue.

In the Android app, I have a Main Activity. Here's the relevant code from said Activity.

installation.saveInBackground(new SaveCallback() {
        public void done(ParseException e) {
            if (e == null) {
                Log.v("PRBTEST","Successful save of installation");
            } else {
                Log.v("PRBTEST","Unsuccesfull save of installation: " + e.getMessage());

                ParseErrorHandler.handleParseError(e);
            }
        }

This code is towards the end of the onCreate() function, and is intended to catch an issue with Parse.com, our backend server, where the session token becomes invalid. The error handler is supposed to check the error, and depending on the type of error, do something about it. For now we only have one context, which is the session token error. In response it gives you a popup telling you to login again, and sends you back to the startup screen where you can login.

Here's the error handler class.

public class ParseErrorHandler {

public static void handleParseError(ParseException e) {
    switch(e.getMessage()) {
        case "invalid session token": handleInvalidSessionToken();
                break;
    }
}

private static void handleInvalidSessionToken() {
    AlertDialog.Builder alert = new AlertDialog.Builder(//...);

    alert.setMessage("Oops! Your session is no longer valid. Please sign in again.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    revertToSignIn();
                }
            });
    alert.create().show();
}

private static void revertToSignIn() {
    ParseUser.logOut();

    Global.loggedIn = false;
    Global.user = ParseUser.getCurrentUser();

    Intent intent = new Intent(//..., ActivityStartup.class);
    startActivity(intent); //<<this line can't be resolved at the moment
    finish(); //<<this line can't be resolved at the moment
}

} A couple issues here. I know I have to pass some kind of context into the methods in ParseErrorHandler so they can be used in the parameters that are currently replaced with //... . Problem is I don't understand for the life of me what these contexts are and how they work and every online explanation gets progressively more abstract. I thought the context was essentially just the Activity that the Intent or AlertDialog is being called from but this must not be correct.

The other issue is that the last couple lines in revertToSignIn() that activate the intent cannot be resolved from ParseErrorHandler. These seem like they should be relatively basic issues but could someone please clearly explain this to me?

3 Answers3

1

I don't understand for the life of me what these contexts are and how they work and every online explanation gets progressively more abstract.

here is I got pretty information of Contexts, will help you.

What is 'Context' on Android?

http://developer.android.com/reference/android/content/Context.html

For your problem first create context in main activity then do like

    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_category);
        context = this;
       .
       .
       .
       .
       .
       installation.saveInBackground(new SaveCallback() {
            public void done(ParseException e) {
                if (e == null) {
                    Log.v("PRBTEST","Successful save of installation");
                } else {
                    Log.v("PRBTEST","Unsuccesfull save of installation: " + e.getMessage());

                    ParseErrorHandler handler = new ParseErrorHandler(context);
                    handler.handleParseError(context, e);
                }
            }

Now create constructor in your ParseErrorHandler.java class which contain context which helps you in all methods do something like that,

private Context context;
    public class ParseErrorHandler {

        public ParseErrorHandler(Context context) {
            this.context = context;
        }

        // your other code...

        private static void revertToSignIn() {
            ParseUser.logOut();

            Global.loggedIn = false;
            Global.user = ParseUser.getCurrentUser();

            Intent intent = new Intent(context, ActivityStartup.class);
            startActivity(intent); 
            finish(); 
        }
    }

Hope thats help you..!!

Community
  • 1
  • 1
Nils
  • 647
  • 6
  • 16
  • This works just as well as the other response. I think this one is a little more elegant actually. In the last few hours since posting the question I've gained a bit of understanding but this has helped as well, thanks. – Matthew Jendrasiak Feb 26 '16 at 14:29
0
**Activity:-**
installation.saveInBackground(new SaveCallback() {
        public void done(ParseException e) {
            if (e == null) {
                Log.v("PRBTEST","Successful save of installation");
            } else {
                Log.v("PRBTEST","Unsuccesfull save of installation: " + e.getMessage());

                ParseErrorHandler.handleParseError(e,ActivityName.this);
            }
        }
**ParseClass:**
public class ParseErrorHandler {

public static void handleParseError(ParseException e,Context context) {
    switch(e.getMessage()) {
        case "invalid session token": handleInvalidSessionToken(context);
                break;
    }
}

private static void handleInvalidSessionToken(Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(//...);

    alert.setMessage("Oops! Your session is no longer valid. Please sign in again.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    revertToSignIn(context);
                }
            });
    alert.create().show();
}

private static void revertToSignIn(Context context) {
    ParseUser.logOut();

    Global.loggedIn = false;
    Global.user = ParseUser.getCurrentUser();

    Intent intent = new Intent(context, ActivityStartup.class);
    context.startActivity(intent); //<<this line can't be resolved at the moment
   context. finish(); //<<this line can't be resolved at the moment
}
Rohit Heera
  • 2,709
  • 2
  • 21
  • 31
  • Thanks! I see, I need to call startActivity from the context that you passed. That helped. I still had to make a couple changes but this got me to some working code. – Matthew Jendrasiak Feb 26 '16 at 14:28
0

Following both of the answers above I got this final code, which is similar but different from their Answers. This compiled and ran great!. There are of course some additions.

public class ParseErrorHandler {
public static void handleParseError(ParseException e, Context context) {
    switch(e.getMessage()) {
        case "invalid session token": handleInvalidSessionToken(context);
            break;
        default: handleGeneralError(e, context);
            break;
    }
}

private static void handleInvalidSessionToken(final Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Your session is no longer valid. Please sign in again.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    revertToSignIn(context);
                }
            });
    alert.create().show();
}

private static void handleGeneralError(ParseException e, Context context) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setMessage("Oops! Fitness Evolution had a server issue.\n\nError: " + e.getMessage() +
            "\n\nSome functionality may be temporarily unavailable. We apologize for the inconvenience")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //If you want OK button to do anything special, put it here
                }
            });
    alert.create().show();
}

private static void revertToSignIn(Context context) {
    ParseUser.logOut();

    Global.loggedIn = false;
    Global.user = ParseUser.getCurrentUser();

    Intent intent = new Intent(context, ActivityStartup.class);
    context.startActivity(intent);
}
}

And the code in ActivityMain.class is :

 Log.v("parse installation", "updating user");
    ParseInstallation installation = ParseInstallation.getCurrentInstallation();
    installation.put("user", Global.user.getUsername());
    installation.saveInBackground(new SaveCallback() {
        public void done(ParseException e) {
            if (e == null) {
                Log.v("PRBTEST","Successful save of installation");
            } else {
                Log.v("PRBTEST","Unsuccessful save of installation: " + e.getMessage());

                ParseErrorHandler.handleParseError(e, ActivityMain.this);
            }
        }
    });

    Log.v("parse installation", "updated user: " + ParseInstallation.getCurrentInstallation().get("user"));

One big thing I didn't realize was that I have to call .startActivity FROM A CONTEXT. Like context.startActivity().

Thanks guys.

Muhammad Nabeel Arif
  • 19,140
  • 8
  • 51
  • 70