Issue
Here's the issue. I have an application where I want to press a button, start a new activity that displays a list of items, allow the user to select any number of items, click submit and send that data to the original activity that called the new activity with the list of items. Here's the basic structure:
Activity A -> Activity B (select items, press submit) -> [already running] Activity A (receive sent items)
I have no problem sending data back and forth. The problem that I do have is that when I try to restart Activity A, it goes through the Activity's onCreate()
method. I prefer not to do this because I want to be able to set up everything beforehand in the onCreate()
method, then supplement what is already in there through the onRestart()
method. I'm not exactly sure why the onCreate()
method is being called every time I restart the activity. My guess is the way I call the activity in Activity B:
Intent intent = new Intent(this, PatientChartActivity.class);
intent.putExtra("checked", checked);
intent.putStringArrayListExtra("checked", checked);
startActivity(intent);
I have been looking at the Android activity lifecycle and I'm not sure why it is not automatically returning to the previous activity, unless I do indeed have to force it do do so. One thing that I am sure of is the the activity that is running is not being destroyed. I put in a Log.v(TAG, "DESTROY")
log message in the onDestroy()
method ensuring it doesn't destroy the activity. I have tried using different flags when I start the activity to tell the system that I want to restore a previously started activity, but those don't seem to work either. I may misunderstand them. I have researched exstensiveley on the topic but none of the solutions I found have helped. Here is an issue that I found that seems to be identical to mine
but didn't seem to solve what I was looking for. I also looked at the following links for other possible routes of getting around the issue but did not work.
http://www.droidnova.com/use-intents-to-start-other-activities,76.html
http://www.warriorpoint.com/blog/2009/05/24/android-how-to-switch-between-activities/
Solution
What it is actually done according to the things you have stated("Activity A -> Activity B (select items, press submit) -> [already running] Activity A (receive sent items)") is:
Activity A = new Instance -> going to Activity B = new Instance -> going to Activity A again = new Instance
So your activity "stack" is actually 2 instances of activity A and one of activity B. That is why your second call in activity A initializes it and calls 'onCreate()' again. One of the solutions to the problem, and probably the best is:
From activity A start the activity B with this method: 'startActivityForResult()'. Then in activity B despite starting the activity A as you currently do, try setting a result back to activity A through this function: 'setResult (int resultCode, Intent data)' and then call 'finish()' in activity B. In activity A implement the 'onActivityResult()' method.
I will now explain how the application lifecycle:
- activity A is initialized and starts activity B waiting for a result in order to do "something" with it. So far so good.
- activity B is initialized and after submit it does set a result for activity A. Then it finishes itself and automatically the result is passed to the previous activity.
- activity A takes focus again, ALREADY INITIALIZED and the 'onActivityResult()' method is invoked. The 'intent' that was set in activity B and now is passed in activity B.
Tell me if that helps and if you need some sample code.
Answered By - 10s
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.