Problem with activity life cycle

Put your problem here if it does not fit any of the other categories.

Problem with activity life cycle

Postby sandis84 » Wed Oct 28, 2009 12:56 pm

What I am trying to do is the following:

I have an activity and a sub-activity that is started by the first activity. When the sub-activity is closed, I want it to check certain variables. Then it will create an object, which will be placed in a global static hashmap. When the first activity is resumed, it should get this object and do certain operations on it.

What I have done is that I have placed the code of the sub-activity in "onStop()", and the code in the activity in "onResume()". However, I have noticed that onResume in the first activity runs before onStop in the sub-activity. So how should I do? I cant place the sub-activities code in onPause, becuase then it would be executed if for example the phone goes into waiting mode/locked mode or whatever it is called.
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Top

Postby wiederke » Wed Oct 28, 2009 2:00 pm

Hi,

I sugget storing data which is used by more than one Activity in the Application object. Checkout the example in this thread: http://www.anddev.org/constant_communic ... t7956.html


bye

torsten
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

Postby sandis84 » Wed Oct 28, 2009 3:38 pm

Thanks man, but it still does not solve my problem. I need something to be done when the sub-activity exits, and continued med the first activity resumes. However, these overlap, which is making things difficult for me.
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Postby Oleg » Mon Nov 02, 2009 10:27 am

Hi. I don't exactly understood your problem. But I think you need to use

Intent intent = new Intent(SubActivity.ths, FirsActivity.class);
Bundle b = new Bundle();
b.putInt("key","value");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
onNewIntent(intent);
startActivity(intent);
finish();

In FirstActivity on onResume method get this "key".
Also if need in Manifest.xml file for FirstActivity and this android:noHistory="true" android:launchMode="singleTask"
Oleg
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Wed Nov 28, 2007 1:49 pm
Location: Ukraine

Postby Emmanuel7 » Wed Nov 04, 2009 2:02 am

I think the proper way to do this is to launch the second activity with the startActivityForResult method.
When the second activity has finished its job; it can create a bundle, put the result inside, put the bundle in a new intent, and call setresult with this intent.
Last step : in the first activity, OnActivityResult will be called, and you can get your result !

Hope it helps,
Emmanuel7
Senior Developer
Senior Developer
 
Posts: 164
Joined: Fri Dec 12, 2008 2:17 am
Location: Paris

Postby sandis84 » Wed Nov 04, 2009 10:53 am

Emmanuel7 wrote:I think the proper way to do this is to launch the second activity with the startActivityForResult method.
When the second activity has finished its job; it can create a bundle, put the result inside, put the bundle in a new intent, and call setresult with this intent.
Last step : in the first activity, OnActivityResult will be called, and you can get your result !

Hope it helps,


Thanks. I had actually forgotten about this thread as some refactoring of my code made the problem disappear. But this should work, I have used the technique in other situations. So others having the same problem should try this =)
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests