tags:

views:

586

answers:

2

Hi, 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.

now i though that i could pass that class(SimpleClass), only MainActivity's context, problem is that, u cant 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 create the instance of SimpleClass 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 its working, but isnt a proper way of doing this? I`am afraid i could have some memory leaks in the future.

thanks. ray.

A: 

I don't know what you are trying to do but you should read this.

Macarse
Hi, it was me who posted this.. but the prob is, that there i didnt need to use startActivityForResult..onyl startActivity..it doesnt work with the same case as here(startActivityForResult)
rayman
+1  A: 

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
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
What about making SimpleClass a nested class?
codinguser