Android: Using search API to do equivalent of startActivityForResult - android

I have an activity that is started from the search api. I would like it to "return" some values to the activity that was running when the search was instigated, in a similar way to startActivityForResult, but I can't see how to do it. Any suggestions?
Activity A -> (startActivity) ->
Activity B -> (Search) -> Activity C
-> (return) -> Activity B
At the moment I'm starting a new instance of activity B using the values. This works, but the activity stack is then not the way it should be. Ie, pressing back from B would go back via C, B, A, instead of just A.
Any suggestions on the way to do this?

I'd like to know how to do this too. The only kludge I can think of is to have public static variables in B to pass the result back in, and then have C call finish() after setting said variables, with B processing the results in the onResume() method.

I've come up with a solution: to combine B and C. Instead of having a separate search handling activity, I can get B to handle the search and pop up a dialog with the results in it. It doesn't feel elegant, I'd much rather have the separate, but it's the best solution I could find. This is assuming, of course, that it works, but I can't currently see a reason for it not to (in my particular situation).
EDIT: Here's the code. EditLocation is A, GetLocationMap is B
https://github.com/spookypeanut/Wake-Me-At/tree/e0dde4153c375c20bec3d7201b4faac2300f5956

Related

Keeping specific activity on top of a backstack

In our app we use an activity C from a library which always should be on the top of a back stack (or should always be visible in case if being on top is impossible).
For example we have an activity A where some asynchronous job is in progress. At some moment the activity C is launched so the backstack looks like this: A -> C. When the asynchronous job in A is done it should launch the activity B in such a way that C would still be visible but B would still exist. It looks like the back stack should be like A -> B -> C or (this looks more possible) there should be two back stacks like this: (A -> B) and (C).
For now we tried some manipulations with overriding startActivity for activity A.
Are there any better ways to achieve the situation that C should be always visible until a user closes the activity?
I have come to the answer. In case you encounter this stuff as well the solution in my case looked like this:
The idea is that I save the state in the activity A after the asynchronous work which is necessary for launching the activity B. If the activity C is on top of the stack, the activity B would not be launched (done via overriding A's startActivity method). Then when the activity C is finished, activity A’s onStart method is launched. And in this method we start Activity B. Visually it looks like what I wanted to achieve.

Back button query, want it to return to a specific page on the stack

I have three activities A, B and C.
A --> B --> C. activity B isn't shown to the user, its calling an api and gathering some information. Should the user hit back I'm hoping for it to go back to A, but not sure how this can be done?
I've already tried coroutines to get around this issue and I've not had much look.
Result is that the back button when pressed continues back to activity C and never gets back to activity A.
Any advice would be great.
Thanks
You can call finish() on B Activity after startActivity(...) call
startActivity(Intent(this, C::class.java))
this.finish()
Why are you using Activity B at all? I think there is a problem with your design. Look into android architecture components project for a good design guideline. Usually things like api calls need to be done inside classes that do not depend on Activity lifecycle, unless you need the activity lifecycle for your requirement. Android Architecture Components "JetPack"

Android three activities, pass data from third activity to parent activity

I have 3 activities. Let's say A, B, C. Main activity is A.
I'd like to start B from A then start C from B, then close B when C opens. Finally upon closing of C pass data to A.
How's this possible especially with Intent and startActivityForResult? Is it at all possible with the two latter methods?
Thanks.
Supose you are using a button, when you click the button to start the activity C, you can finish the activity B, and get the action in the activity A.
Use onActivityResult() on activity A. On this pont, you can start the activity C using startActivityForResult().
This way, when you finish C, you can pass data to A, again using onActivityResult().
Renan Lopes Ferreira's reply did the trick. Cheers.
You can always start B from A and C from B using Intents.
As you are removing A from the stack, C cannot directly pass data to A using startActivityforResult() and onActivityResult). (Isn't it obvious, A doesn't even exist in memory).
So for A to access C's data, you must store C's data somewhere, so that A can access it, the next time it is up. Now, here the other scenario would be store it as persistent or non-persistent data. If you want the data to be persistent, it is recommended to use Shared Preferences, Database etc. and if you want it to be non-persistent then you may use Singleton etc.
Refer : http://developer.android.com/guide/faq/framework.html

Android - how to start chain of activities?

I intend to start 3 activities in a chain (like from main open Activities A, B and then C, which will be visible for the user), but I wasn't able to find some way how to do that in Android. Do not ask me why, I just have to do that for restoring my application state, where is was before.
Thanks for any ideas
Waypoint
Edit:
Ok, I have tried opening activities in For cycle, but they aren't opened properly. They are chained, but recreated only when I press back button and they display to me. I need some solution which leads to: open A, if A is opened check if needs to open B -> YES, open B, check if needs to open C -> YES, open C, no need to open another activity -> FINISH
prior to start any activity , decide which activity should be start .
Lets take your case >>first check for B , if yes check for C , now open the required 1 .
i understand comparable data is inside the activities, but a right data structure will always allow you to access the data wherever and whenever it actually requires .
For future readers: if you want to start activity with proper back stack, you should use TaskStackBuilder.
define the natural hierarchy for your activities by adding the android:parentActivityName attribute to each element in your app manifest file
create an instance of TaskStackBuilder and call addNextIntentWithParentStack(), passing it the Intent for the activity you want to start.
For more details see official documentation
Override the onResume-method in each activity. Add the check and the start of the activity there.
public void onResume() {
if( condition )
startActivity( intentForTheNextActivity );
}
Where condition is whatever condition you might have (in your example if B should be started, C should be started etc.) and intentForTheNextActivity is the intent for the following activity in the chain (e.g. if now in A, the intent is for B etc.).
I'm having a very hard time understanding exactly what it is you're trying to do. Sometimes it seems like it's a chain (A opens B, B opens C and so forth) sometimes it seems you want some random flow (A opens B, B opens A, A opens B, B opens C) - which makes it really hard to give you a specific answer.
What I can do, is recommend that you read up on the following:
Activity Lifecycle
Starting Activities and getting results (in particular the methods startActivityForResult and setResult)
If you need more help than this - you need to explain yourself better (maybe with a diagram or some sample code of what you have tried so far).
You didn't provide any information of what you've tried, so i'll give you the simplest answer:
method startActivity(Intent intent), more info here.
Edit: Hmm, how about this? I don't have SDK around me now, but i can provide a concept. I'm not sure if it works, but i hope it'll guide you to soultion.
Let's imagine this is ActivityA's code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (doINeedActivityB) {
Intent activityC = new Intent(this, ActivityC.class);
this.startActivity(activityA);
}
if (doINeedActivityC) {
Intent activityC = new Intent(this, ActivityC.class);
this.startActivity(activityC);
}
}

android: Activity.startActivityForResult() or ActivityGroup?

I have the following situation:
activity A -> resultA
activity B -> resultB
activity C -> resultC
and I need all the results (A, B & C) to build the final out put?
What's the better way:
AA extends Activity {
startActivityForResult(A);
startActivityForResult(A);
startActivityForResult(A);
}
or to make AA subtype of ActivityGroup?
Activities A, B and C do not require to be visible (foreground) at the same time.
Thank you!
IMHO, none of the above.
Popping out of one activity into another briefly via startActivityForResult() is fine...once. It gets cumbersome for users and developers alike when there are several.
Consider recasting your UI as a wizard, perhaps driven by a ViewFlipper, so you can keep all of this in a single activity. It also provides the user with a recognized pattern for going back and forth within the chain of steps leading to them providing all of the information you need.

Categories

Resources