90

I have MainActivity which is an Activity and other class(which is a simple java class), we'll call it "SimpleClass".

Now I want to run from that class the command startActivityForResult.

I though that I could pass that class (SimpleClass), using only MainActivity's context, but the problem is that we can't run context.startActivityForResult(...);.

So, the only way making SimpleClass to use startActivityForResult, is to pass the reference of MainActivity as an Activity variable to the SimpleClass.

Something like that:

Inside the MainActivity class I created the instance of SimpleClass like this way:

SimpleClass simpleClass = new SimpleClass(MainActivity.this);

Now, this is how SimpleClass looks like:

public Class SimpleClass {

Activity myMainActivity;

   public SimpleClass(Activity mainActivity) {
       super();
       this.myMainActivity=mainActivity;    
   }
....


    public void someMethod(...) {
        myMainActivity.startActivityForResult(...);
    }

}

Now it's working, but isn't there a proper way of doing this? I am afraid I could have some memory leaks in the future.

Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
rayman
  • 20,786
  • 45
  • 148
  • 246
  • I don't know what you are trying to do but you should read [this](http://stackoverflow.com/questions/2253088/passing-activity-to-non-activity-object-properly). – Macarse May 17 '10 at 12:06
  • 1
    By doing this could you use the `onActivityResult` method inside your simpleClass or you did not intend to? [Same problem here..](http://stackoverflow.com/questions/25557087/how-to-get-the-result-from-onactivityresult-inside-another-classoutside-of-the/25557182?noredirect=1#comment39915501_25557182) – Vivere_FlowCoder Aug 29 '14 at 08:27

3 Answers3

68

I don't know if this is good practice or not, but casting a Context object to an Activity object compiles fine.

Try this:

if (mContext instanceof Activity) {
        ((Activity) mContext).startActivityForResult(...);
} else {
        Log.e("mContext should be an instanceof Activity.");
} 

This should compile, and the results should be delivered to the actual activity holding the context.

Sakiboy
  • 7,252
  • 7
  • 52
  • 69
Some Noob Student
  • 14,186
  • 13
  • 65
  • 103
4

If you need to get the result from the previous Activity, then your calling class needs to be of type Activity.

What is the purpose of you calling Activity.startActivityForResult() if you never use the result (at least according to the sample code you posted).

Does myMainActivity do anything with the result? If so, then just make SimpleClass a subclass of Activity and handle the result from within SimpleClass itself.
If myMainActivity needs the result, then maybe you should refactor the code to start the activity from myMainActivity.

codinguser
  • 5,562
  • 2
  • 33
  • 40
  • 2
    I am using result, just i wanna seperate it to diffrent class, so i was wondering how should i pass an Activity parameter to a non-activity class, without causing leaks in the future. – rayman May 30 '10 at 11:06
  • 1
    What about making SimpleClass a nested class? – codinguser May 30 '10 at 20:38
1

Better solution is :

  1. Making SimpleClass a subclass of your Activity class
  2. calling another Activity as startActivityForResult
  3. handling the result within SimpleClass itself
gtiwari333
  • 24,554
  • 15
  • 75
  • 102
  • 3
    sounds like this should work, but I got this: `java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference` – Boy Jan 28 '16 at 09:22