I have a situation in which I have 3 activities.
ResultsActivity
SimpleSearchActivity
ComplexSearchActivity
The flow works as follows: The user can choose to search from the ResultsActivity which will launch the SimpleSearchActivity. From there the user can perform a simple search. The result is pass back to the Results activity.
However from the SimpleSearchActivity the user can choose to do a complex search which will finish the SimpleSearchActivity and launch the ComplexSearchActivity. From there I want to pass the result back to the ResultsActivity. I am not sure how to do this since this 3rd activity was not launched from the first activity, but the second.
Options I have considered:
In complex search case go back to ResultsActivity and launch the ComplexSearchActivity from there. Not sure I really want this as I don't want the SimpleSearch to close go back to the Results then immediately launch the Complex. I am worried this will 'flash' the ResultsActivity before launching the ComplexSearchActivity.
In complex search case, from SimpleSearch launch ComplexSearch with startActivityForResult(...), on complex finish the simple search can grab the result, then pass that back to the Results activity. I am not sure this chaining will work. Even if it does I am worried that on complex finish the SimpleSearchActivity will 'flash' just to pass the results back.
Store the search results in a static variable somewhere and on ResultsActivity launch just check for that static variable that might have been set by either search activity. Yuck!
I have also thought of only having one search activity and doing a show/hide on certain fields (simple vs complex). However I launch the simple search as a dialog activity (in tablet case) and complex search as full screen activity. So I cannot really use the same search activity.
Ideas? Has anyone tried something like this before?
There are a couple ways you can achieve this:
Option 1: You can choose not to close SimpleSearchActivity when you launch ComplexSearchActivity. Instead, have ComplexSearchActivity send its result back to SimpleSearchActivity, and from there have SimpleSearchActivity pass that result to ResultsActivity.
Update: This will, at least in my experience, have no UI "flash"; it should appear seamless.
Option 2: Use FLAG_ACTIVITY_FORWARD_RESULT, which exists for exactly this purpose.
Have you think about the approach the abstract the data layer to be shared across all activities rather than to have only certain activity to hold the data?
Related
I'm developing an android app that is going to be used on a information screen. This app contains 5 (weather, next run, opening hours, etc...) activities, the plan is to loop through these activities with a timeinterval. So it starts with first activity, sleeps for like 5 seconds and then jump to next activity, and when it comes to the last activity, it should start in the front again.
What is the best solution to create a navigation system like this? Have Android some special features for things like this, or is the default startActivity() the only way?
And there is a little twist with this, some of the activities needs to retrive data from external sources by HTTP. So the activities must be done with data-query before it starts the activity.
You use startActivity to swap between activities. After you are finished with an activity, remember to call finish() - otherwise there will be a lot of instances of the same activity.
But if you want something like an information screen, you could use a single activity but use Fragments. That way you simply inflate a single fragment, and keep a timer to swap between them. You can still show the same content, but there are some differences between activity and fragment in inflating and finding the views by id.
I've got four activities which the application cycles through. The first one fetches huge amount of data, so I don't wish to do that more than once. Thing is that if user presses back key on last activity, I want to return to the first one without reloading the activity. I'm currently thinking startActivityForResult methods and finish the two previous ones, but there may be a better solution?
Scenario:
Hasslarn,
The first thing that you must understand is that you have very little choice as to whether an Activity will be reloaded or not. That is determined by the system (largely). With that said, there are a number of things you can do to limit the system's desire to kill the Activity. Additionally, you may use a number of tools at your disposal to limit the impact of such a possible closure
Finish every child Activity as it becomes unimportant. This will free resources lowering the need to get rid of unused Activities (even temporarily).
Find a simple, but clever way to limit loads.
Based on the information provided, your proposed solution is a viable way to accomplish both. However, I would ask "Is Activity B required to be active while Activity C is open?" If not, you may want to do the following:
startActivityForResult(Activity B)
When Activity B is done, send result back to Activity A and startActivityForResult(Activity C)
Finally, when Activity C is done, you may startActivityForResult(Activity D) and back will work with no effort and you won't have to close the other activities.
Furthermore, if you press Back on Activity C, you can supply a cancelled result to Activity A forcing a restart of Activity B if you so desire. Depending on the required processing for Activity B, this may not be any hassle at all for your app.
Regardless of how you approach this, I recommend finding a way to cache this data such that onResume(), you may reload quickly if needed. This is because there is NO way to ensure that your original Activity will not be released to make way for the others.
Hope this helps,
FuzzicalLogic
Try using this:
Intent a = new Intent(this,A.class);
a.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(a);
return true;
Activity A will get reordered to front, without creating a new instance. If you want to pass extras through intent , you can get the intent extras in onNewIntent(intent) in Activity A.
I have implemented a search-function in my sourcecode which will be started when the user clicks the search-button on his device or the one in my actionbar. If he clicks the one on my actionbar I simply call onSearchRequested. Now here is my problem:
When the Search is done I display the results in a ListView. If the user clicks on an Item I want do go back to my MainActivity (which will always exist when the app isn't finished) and modify some values there und call a method of Main.
I know that there is startActivityForResult but I don't know how I could implement that because if the User hits the search button of his device the app automatically calls onSearchRequested... So the only solution that comes to my mind would be to implement a static method in my Main-Activity which I can call from within the Activity displaying my search results. But that isn't such a nice solution (or?)!
To summarize my question: is there an elegant way to get back from my activity displaying my search results to my main-activity and transfer data without startActivityForResult or implementing a static method in Main?
Thank u very much!
Implementing startActivityForResult is pretty easy. This might help you.
I have a reservation wizard in android, when i say wizard i mean i have multiple activities (meaning screens) that are passed from one to another until booking is complete. in each step of the way my info may be invalid and so ill have to back track 2-3 activities and start all over, all the activities i back tracked should be destroyed, they may need to invalidate a singleton data container that they filled with their data.
I thought of 2 ways to do this:
start all activities related to the wizard with startActivityForResult() so i get notification when an activity is finished and with which error code (finished OK or error occurred) and act accordingly (for instance: if in the middle an activity fails it will return failed and the previous one will return failed as well until i get to the wizard's first activity where i either declare the error with a dialog or show the 'thank you' screen.)
I can send an react on intents with certain parameters so i have a 'close and clean' intent message that close down the previous activity etc...
which way is better, any other interesting\efficient way to do this ?
I Got a hint that got me thinking in a different direction:
use an invisible Activity, or alternatively , a local service,that is the 'brains' that known on each turn of the way what action is to be taken. to remove a complete stack of unneeded activities i should use the FLAG_ACTIVITY_CLEAR_TOP on the bottom most activity i want to use next.
Another options is to enclose all
the activities i want to play with
their pposition on the stack inside
an ActivityGroup Object which keeps
all activities alive (like in Tab)
and on the stack, but you control
which one is displayed and which is
not. the activity group will contain
the state machine logic regarding
which Actvity should not be
displayed and which should be
disposed.
I'm writing a simple Android app, and I'd like better control over the navigation/relationship between the activities. I don't want my activities to act like android activities...I don't want them to stack up within the Task. I want one Activity (let's call it MainActivity) to be the landing point and always be at the bottom of the stack, and I want only one instance of my second activity (call it SecondActivity) to be above it in the stack...would be nice to reuse it as well. I thought I could get this behavior by making MainActivity be the "main" Activity, and declare them both as launchMode=singleTop. This isn't working at all. I provide navigation between them using menus, so when I go back and forth a bunch of times and back out of the app, I go through the whole stack.
How's the best way to have fine control over the Task's Activity stack? I want MainActivity to always back out of the app, and SecondActivity to always back into a single instance of MainActivity. As well, I'd love to get singleTop working so I would use onNewIntent instead of creating and destroying every time. Using the manifest as well as the intent flag is just not working. Any ideas?
Well, you could always just call "finish()" within whatever Activity is calling another activity after the "startActivity()" call. I would definitely advise against trying to stuff an entire app into two activity classes and try to swap views based on what they're doing. If it's that important to you, just close your activities as you launch new ones (obviously not the MainActivity, though).