Restart already running work

Here is the problem. I have an application in which I want to press a button, start a new action that displays a list of elements, allows the user to select any number of elements, click "Submit" and send this data to the original activity that triggered the new action using the list of elements. 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 occurs to me is that when I try to restart Activity A, it goes through the Activity onCreate() method. I prefer not to do this because I want to be able to pre-set everything in the onCreate() method, and then add what already exists using the onRestart() method. I'm not quite sure why the onCreate() method is called every time I restart the action. I suppose I call activity in Activity B:

 Intent intent = new Intent(this, PatientChartActivity.class); intent.putExtra("checked", checked); intent.putStringArrayListExtra("checked", checked); startActivity(intent); 

I look at the life cycle of Android activity, and I'm not sure why it doesn’t automatically go back to the previous action, unless I really need to get it to do it. The only thing I'm sure is that the work that works is not destroyed. I put the Log.v(TAG, "DESTROY") log onDestroy() in the onDestroy() method, ensuring that it does not destroy the action. I tried to use different flags when I start the activity to tell the system that I want to restore the previously launched activity, but they do not seem to work either. I can get them wrong. I researched exstensiveley on this topic, but none of the solutions I found helped. Here , this is a problem that, it seems to me, is identical to mine, but does not seem to have decided what I was looking for. I also looked at the following links for other possible solutions, but didn't 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/

+4
source share
2 answers

What is actually done in accordance with the things that you indicated ("Activity A β†’ Activity B (select items, click" Submit ") β†’ [already running] Activity A (receive sent items)":

Activity A = new instance β†’ transition to activity B = new instance β†’ transition to activity A again = new instance

Thus, your activity β€œstack” is actually two instances of activity A and one of types B. Therefore, your second call in action A initializes it and calls β€œonCreate ()” again. One solution to the problem and probably the best:

From action A, start action B using this method: "startActivityForResult ()". Then in action B, even though you start action A, as you are doing now, try returning the result to activity A through this function: "setResult (int resultCode, Intent data)", and then call "finish ()" in operation B. In action Deploy the onActivityResult () method.

Now I will explain how the application life cycle:

  • activity A initializes and starts activity B, waiting for the result to do something with it. So far so good.
  • activity B is initialized, and after sending it sets the result for activity A. Then it ends and automatically passes the result of the previous activity.
  • activity A focuses again, ALREADY INITIALIZED, and the onActivityResult () method is called. An β€œidea” that was set in activity B and is now transferred to action B.

Tell me if this helps, and if you need some sample code.

0
source

Instead

 startActivity(intent); 

in your activity A, use

 startActivityForResult(intent); 

Thus, you will get the result when this specific activity is completed. Override this in step A:

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //TODO handle here. } 

When action B completes, call this.finish (). It will then return to Activity A and call the method above, which means that your onCreate in Activity A will not be called.

0
source

Source: https://habr.com/ru/post/1435700/


All Articles