-3

I am relearning Java, and am coming across the term ctx in some contexts. Specifically in Android code.

Example from here:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void goToFb (View view) {
        startActivity(newFacebookIntent(this.getPackageManager(), "http://facebook.com/biddingo"));

    /**
     * <p>Intent to open the official Facebook app. If the Facebook app is not installed then the
     * default web browser will be used.</p>
     *
     * <p>Example usage:</p>
     *
     * {@code newFacebookIntent(ctx.getPackageManager(), "https://www.facebook.com/JRummyApps");}
     *
     * @param pm
     *     The {@link PackageManager}.
     * @param url
     *     The full URL to the Facebook page or profile.
     * @return An intent that will open the Facebook page/profile.
     */
    public static Intent newFacebookIntent(PackageManager pm, String url){
        Uri uri = Uri.parse(url);
        try {
            ApplicationInfo applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
            if (applicationInfo.enabled) {
                // https://stackoverflow.com/a/24547437/1048340
                uri = Uri.parse("fb://facewebmodal/f?href=" + url);
            }
        } catch (PackageManager.NameNotFoundException ignored) {
        }
        return new Intent(Intent.ACTION_VIEW, uri);
    }
}

I get an error in my code and can't find any information anywhere regarding this.

Is it some sort of placeholder?

Edit: Replacing ctx with this seems to prevent the error message, but doesn't seem to fix the code correctly. The facebook app opens, but to a blank page and doesn't load anything else

Community
  • 1
  • 1
juil
  • 2,408
  • 3
  • 18
  • 18
  • 2
    ctx = context ... so you need a context of something. – Tom May 25 '16 at 15:41
  • 2
    "I get an error in my code" -- please provide a [mcve], including the code plus details about the error. We cannot help you with code that we cannot see and for an error that you decline to describe. – CommonsWare May 25 '16 at 15:43
  • @CommonsWare Updated the question with the specific example from the link I had previously posted. – juil May 25 '16 at 16:16

1 Answers1

1

Since getPackageManager() is a method on Context, presumably ctx refers to an instance of Context. Your Activity is a Context, for example.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I'm still new to the idea of Context. Even after doing some reading on it, I can't quite figure out what to replace `ctx` with. – juil May 25 '16 at 16:27
  • @rhaphazard: Since did not provide a [mcve], and so we have no idea where this line of code is in your app, we cannot give you much in the way of advice. – CommonsWare May 25 '16 at 16:43
  • Updated with more context. – juil May 25 '16 at 16:52
  • @rhaphazard: Your code seems fine. What does "doesn't seem to fix the code correctly" mean? – CommonsWare May 25 '16 at 16:56
  • It still doesn't work as expected. The facebook app opens, but to a blank page and doesn't load anything else. – juil May 25 '16 at 16:57
  • 1
    @rhaphazard: That has nothing to do with your code, assuming that your URL is valid (I don't use Facebook, so I have no idea if it is or not). Perhaps the answer that you are using is out of date, and that Facebook's app does not support that protocol anymore. That answer relied on reverse-engineering the Facebook app, and so Facebook is not obligated to support it. AFAIK, Facebook has an actual SDK, so you might consider using that. – CommonsWare May 25 '16 at 17:01
  • Thanks for the heads up. I'll look into that. – juil May 25 '16 at 17:30