Find the next operation in onStop () when calling startActivity

how can I determine which function will be next in the onStop () method of the current Activity after onStop () is called by starting a new Activity with startActivity ()?

My only idea at the moment is: Declaring the intent, which I use as a global variable, and checking it in the onStop () method with the intent.getClass () function or something similar. Will it be okay? Somehow it seems fast and dirty.

Thanks for any help!

Edit: So here is the scenario:

Activity1 is a list in which the user selects one or more lines and then clicks "send", all selected lines are now sent to the server in the background job.

I use an array in my service to save the selected identifier before subsensing, so if a user leaves the list without getting into this array, it will still be filled with outdated choices.

So, if the user selects one or more rows and then leaves Activity1 without sending, I want the selection to be discarded, so I call the method to clear this array in the onStop () method of Activity1. This works well, but of course there are different ways to leave Activity1. For example, I have a context menu with 3 entries, and each of them triggers a different action.

Now I need to save the values โ€‹โ€‹of the array in case of only one special separate action (for example, Activity2) from the context menu.

Sorry if this was unclear, but I'm not talking about instances of activity, but about how to behave in general.

So, all I need to know in the onStop () method is if we switched to Activity2.java or Activity3.java from Activity1.

Maybe onStop () Activity1 is not even suitable for my clear or incomprehensible solution?

Thanks again

+4
source share
1 answer

You might want to add some clarification about your intention here, as the question is a bit vague.

By default, when you start Acitvity, it is placed in a queue called the reverse stack. When it ends at the end of an activity, the previous activity is brought to the fore.

Be careful when starting a new action, if you do not configure it correctly, you will create a new instance of this Activity, even if it already exists in the background stack. If I understand your question, you would be looking for:

Intent intent = new Intent(getContext(), NewClass.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); 

This will clear the stack, with the exception of the action mentioned, and bring it to the foreground if it exists in the background stack.

This SO thread shows how to clear the stack ( Android manual ).

Look here for a description of how to manage tasks and activities, but Google warns about default management of beahviour.

+1
source

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


All Articles