filmmaker3d / droidar

Automatically exported from code.google.com/p/droidar
0 stars 0 forks source link

How to run a new activity from a setup #30

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Trying to launch a new activity from a setup by pressing on an object 

What is the expected output? What do you see instead?

I want to launch a new activity from a setup by clicking on an object. I've 
created a new Command and within the execute tried to launch a startActivity 
but I allways have a null pointer exception error when trying to launch an 
intent with startActivity. I've extended the Setup class with Activity so I 
could be able to do that, any sugestions on how to do that? 

What version of the product are you using? On what operating system?
Latest version on Windows 7.

Please provide any additional information below.

I know this isn't really an issue, but I've been trying to achieve this for 
some time without any success, if you could help me I'd be most appreciated!

Original issue reported on code.google.com by dourado...@gmail.com on 6 Feb 2012 at 3:24

GoogleCodeExporter commented 9 years ago
Solved it! Found out that to call an activity from a non-activity context had 
to create the construtor of the setup to get the Context, use that to call the 
startActivity and also pass a FLAG_ACTIVITY_NEW_TASK on the intent. Here's and 
example if anyone wants to do it and doesn't know how:

 public GameSetup(Context context) {
                this.context = context;

     }

and then wherever you want to do it:

Intent newActivity=new Intent(getActivity(),exampleActivity.class);
                newActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(newActivity);

My best regards,
Dourado

Original comment by dourado...@gmail.com on 6 Feb 2012 at 5:19

GoogleCodeExporter commented 9 years ago
From anywhere in the setup you should be able to call 

getActivity().startActivity(new Intent(getActivity(), MyOtherActivity.class));

i think if you call startActivity via an activity and not a context object the 
flag is not necessary. Can you try this? For me it always worked but maybe you 
have a scenario which I never had.

You might also want to read the javadoc of the system/ActivityConnector.java 
class (and maybe take a look into the code), I created this class to make it a 
little easier to pass objects between activities without storing them in any 
static fields.

Original comment by simon.heinen on 6 Feb 2012 at 11:23

GoogleCodeExporter commented 9 years ago
Hi there Simon, sorry for the delay in responding but I only had the chance to 
test your way a few moments ago and it also works great! And yes you are right, 
the flag is not needed if the context object isn't used. I will read the 
javadoc and also check that class ;). 

Thank you for your quick response and for providing such a nicely built 
framework :)!

My best regards,
Dourado

Original comment by dourado...@gmail.com on 7 Feb 2012 at 4:44

GoogleCodeExporter commented 9 years ago
No problem :)

Original comment by simon.heinen on 7 Feb 2012 at 2:08