tags:

views:

88

answers:

4

I have a main activity and a sub activity. The main activity starts the sub activity using startActivity, and passes an object in the intent. The sub activity reads the object out of the intent in its onCreate action. The sub activity updates the object, then returns to the main activity using startActivity, again passing the updated object back. However, the main activities onCreate function is not called, so the code it contains to read the passed object does not run.

Further investigation indicated that the main activity onPause event is firing, i.e. it is only paused when the sub activity runs, so when the sub activity starts the main activity again, it just onResumes.

Does anyone know if there would be any disadvantages if I moved my data restore/store activities to the onResume and onPause events? I'm not using the onCreate savedInstanceState, should I be?

How else do you pass a set of data items between Activities without using a database or those preferences? Should I be using a database? I have about 20 fairly individual data items.

Any help would be much appreciated,

  • Frink
A: 

It is more likely calling onResume() or onStart() depending on the child activity and how much control it takes.

Look at this page about 3/4's down at the graph.

http://developer.android.com/guide/topics/fundamentals.html alt text

The parent never gets destroyed so it wouldn't have to recreate it. It should just get paused. You can override onResume() and onStart() just as you would onCreate().

Edit: Is there anything given to the user in the subactivity? It sounds like you don't actually need an activity you just need a java class that you can call methods on.

Mike
I have a load of variables (in a class) which I pass to the sub activity, the user updates them and sends it back, so I do have a ui in the sub activity.How would the diagram look if you included a sub activity?
FrinkTheBrave
It would follow down the line. Look after the green Activity is running node where it says Another activity comes in front of the activity. That other activity is your sub activity. Just override onResume and onStart and add a toast to each of them to say which one got called and you will know which one is getting called.
Mike
+1  A: 

Have a look at the Activity life cycle here.

Also, consider starting your sub-activity using StartActivityForResult.

Asahi
It is unclear from the lifecycle what real-world events cause the various onCreate etc. methods to be called
FrinkTheBrave
+5  A: 

I would check out the startActivityForResult() method rather than just startActivity()

That should give you a means to pass things back to the calling activity.

From http://developer.android.com/reference/android/app/Activity.html:

Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startActivityForResult(Intent, int) version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, Intent) method.

Chris Thompson
Thanks, I'll try that. But how does the sub-activity pass the intent back to be read in onActivityResult in the main activity?
FrinkTheBrave
Also, how do I pass back the resultCode used in onActivityResult?
FrinkTheBrave
Check out the example here: http://developer.android.com/guide/appendix/faq/commontasks.html#opennewscreen
Chris Thompson
Cheers, I see it now. However, the line it uses is:setResult(RESULT_OK, "Lincoln", stats);But the reference at developer.android.com/reference/android/app/Activity.html#setResult(int) only shows one or two parameters. Any ideas?
FrinkTheBrave
My guess is that the documentation from the FAQ is outdated. The one with two parameters would be good because it would allow you to create an intent to pass back. You could also use eclipse to figure out what options you have available in your particular build.
Chris Thompson
Could I avoid all this by the sub activity just altering a static variable in the main activity:MainActivity.myStaticVar = 12345;
FrinkTheBrave
This seems to work. If I update MainActivity.myStaticVar in a sub activity, when it returns to MainActivity the data is updated, even if you press the Back button rather than any button I have written which calls setResult. Seems much easier than passing data in Intents. I think I'll raise this as a new question and see if anyone else has any thoughts
FrinkTheBrave
Interesting idea. Personally I think it's dirty, but hey, if it get's the job done... :-)
Chris Thompson
I've raised this as 'Can I update data members in one Activity from another Activity in the application?', where Timothy Lee Russell points out that MainActivity could be deleted by the system when SubActivity is running, so I guess my method can't be used after all. Shame
FrinkTheBrave
@Fink ah that's a good point. Doing it this way by passing an intent back is far more extensible as well. I'm willing to bet you'll be thankful you did it this way later. Good luck!
Chris Thompson
A: 

Why don't you call startActivityForResult from the main activity and get the data back in onActivityResult ? This is the normal way to do it.

fedj