use startActivityForResult from non-activity

Posted by rayman on Stack Overflow See other posts from Stack Overflow or by rayman
Published on 2010-05-17T11:56:47Z Indexed on 2010/05/17 12:00 UTC
Read the original article Hit count: 343

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about android

Related posts about android-sdk