Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult outside of an activity scope

Tags:

android

pojo

I'm trying to create an android project that contains shared code which will be used by others. In this project I only have POJO and no android specific classes.

Some of the functionality requires calling some activities and is depended on the result. My POJO classes get reference to the calling activity when used, but that's happening at run time and I have no control over the implementation of those activities.

My problem is that with the reference of the calling activity I can startActivityForResult but I have no way of adding the onActivityResult, which might exists in the calling activity but is not aware of the requestCode I used.

My question then is how do I know, from within a regular java object when the activity has returned? since, as far as I understand I can only implement onActivityResult on Activity classes.

thanks!

like image 742
Nitzan Tomer Avatar asked Oct 04 '11 14:10

Nitzan Tomer


People also ask

Can we use onActivityResult in fragment?

We can call startActivityForResult directly from Fragment but actually mechanic behind are all handled by Activity. Once you call startActivityForResult from a Fragment, requestCode will be changed to attach Fragment's identity to the code.

What can I use instead of Startactivity for results?

We use startActivityForResult() to send and receive data between activities, in almost of our android projects. But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for it.

What is called after onActivityResult?

onActivityResult, called after onResume, The basic reason that when your jump from your activity to another activity without calling finish. Then last activity went in onStop state.

Can startActivityForResult still be used?

It has deprecated startActivityForResult in favour of registerForActivityResult . It was one of the first fundamentals that any Android developer has learned, and the backbone of Android's way of communicating between two components.


2 Answers

You will have quite a few problems with this setup, but this is how you might want to get started:

You will have to include an activity in your project which does nothing else than starting the activity you want to get the result from and stores it in a globally accessible storage (e.g. a singleton or a static field).

class Pojo {
    static final ConditionVariable gate = new ConditionVariable();
    static int result;

    int m(Context context) {
        context.startActivity(new Intent(context, ForwarderActivity.class));
        gate.block();
        return result;
    }
}

class ForwarderActivity extends Activity {
    private boolean started = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!started) {
            started = true;
            startActivityForResult(new Intent("ContactsProvider"), 1);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Pojo.result = resultCode;
        Pojo.gate.open();
    }
}

There are a couple of problems, though. Like your POJO's method can't be called from the main (UI) thread, because you need to convert an asynchronous call (startActivityForResult()) to a synchronous one (Pojo.m()) and the activity you want to receive info from will be started on the main thread, so you can't block it in Pojo.m()...

Anyway, the code does not work, but you can see which way to go if you really have to stick with this setup. But you should really try to come up with some other means of fetching the data, like a content provider.

like image 178
Szabolcs Berecz Avatar answered Sep 30 '22 00:09

Szabolcs Berecz


i have the same problem while i play with UNITY3D,the unity have it's own Activity(the unity player),i don't wanna edit it for some reason. but the player activity do nothing inside the "onActivityResult" function. And i have something to do when access image picker,i can call "unityPlayer.startActivityForResult" to open image picker,but NO WAY TO CODE MY OWN "onActivityResult".

i think what we hope is something like this:

OtherActivityClass.onActivityResultListener=function(){My Own CODE}..........OR OtherActivityClass.onActivityResultListener.add(myResultListener)..................

like image 24
user3608147 Avatar answered Sep 29 '22 23:09

user3608147