0

I have successfully implemented the firebase dynamic links but failed to get the shorten link out of task so that it can be used in other class any help? this is my code sofar:

Uri Shortlink;
protected void onCreate(Bundle savedInstanceState)
{
   Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
      .setLongLink(Uri.parse(linkHere))
      .buildShortDynamicLink()
      .addOnCompleteListener(this, new OnCompleteListener<ShortDynamicLink>() 
       {
         @Override
         public void onComplete(@NonNull Task<ShortDynamicLink> task) 
         {
           if (task.isSuccessful()) 
           {
              // Short link created (THIS WORKS FINE)
              Shortlink = task.getResult().getShortLink();
              Log.i("CHEK 1", "shortLink = " + Shortlink);
            }
           else 
            {
               // Error
            }
          }
       });

    // I want this shortlink out of this task here. But i am getting (Null) 
    Log.i("CHEK 2", "shortLink = " + Shortlink);
 }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Clement
  • 35
  • 3

1 Answers1

2

Tasks are asynchronous. You can only get the results inside a listener added to it. Trying to make the task wait until its results are ready is a bad idea because you would be blocking the main thread. And that could cause your app to ANR.

You should rewrite your code to deal with the results of the task inside that listener, as you are doing with the CHEK 1 line.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441