I have two activities, Activity A has a button "Load image" which should let the user select an image, but I want to process the chosen image on an other activity B.
I think this is a common scenario, how should I handle the activity flow??
This is what I came so far(which I don't like)
Activity A--> Gallery --> Activity A --> Activity B
Activity A's onActivityResult() only gets the URI and starts Activity B, so It kind of a waste to recreate the activity just to execute 5 lines and be destroyed (insn't it?). I would like something cleaner or more direct. This is just an example, anything is welcome:
Activity A--> Gallery --> Activity B
EDIT
What about this?
Activity A--> Activity B--> Gallery--> Activity B
Is it a better approach or is it the same thing? I mean, will i gain something (performance, cleanness).
EDIT 2
As #Gaurav said, a workaround option would be to use 2 layouts instead of 2 activities thus no need to directly deal with my problem. I must say this should do for me as activity A is very thin. But for the sake of knowledge, i will welcome a "direct" answer to my question.
EDIT 3
Finally, having both layouts in the same activity didn't work (though it was a very attractive solution). My Activity B is based on libgdx (opengl), and i'm getting some nasty deadlocks when onDrawFrame is not called. So to avoid future untraceable bugs of this kind, I'll separate my activities. I'm going for Edit-1 solution, any comments would be appreciated.
I think there is no other way than to call Activity B in the onActivityResult of A.
Related
I have an activity A. Activity B is partially transparent (so I can see activity A below).
Is there an option in Android to allow a user to interact with Activity A while B is still in front?
I want to leave activity B in front the whole time, but allow the user to interact with screen A.
overriding the implementation of onTouch or onClick in B so I can pass the event to activity A?
This is not possible and will not be, because it's a security concern. This on web is called clickjacking. The reason that this is not possible is because you could theoretically manipulate the user to do something unintended like deleting a file or similar, just by guiding them on where to click on a fake screen.
in my experience there is nothing like this. So, you must close activity A and B can be clickable or something.
I have this situation that I am not sure about the right design/way of doing things.
I have an activity where user would spend most of his time (Call it Activity A). Then the user can go to another activity where it is more graphics intensive (Call it activity B). Activity B would have around 40 Imageviews that have looping drawable animations. The user will be navigating back and fourth between those activities multiple times.
Is the expectation to create Activity B every time the user navigate to it and reinitalize the the 40 views based on the stored Data in my application class (it has coordinates and the type of view created)?
Or is there better way?
Thank you
You can use Following flag with intent to resume same activity without recreating it:
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Instead of using activities, you can use fragmentA and fragmentB
When you switch between two fragments, data of them will not be lost.
learn about fragment here
you can use fragments with in the single activity. fragments are easy to swicth between another fragment. so did not lose data of another fragment.
I have an Android app with multiple activities. The main activity communicates over a network and can launch or dismiss various other activities depending on commands it receives over the network. When an Activity is dismissed I don't want to finish() it, just move it down the stack so it's no longer the top activity. What I really need is a FLAG_ACTIVITY_REORDER_TO_BOTTOM but there is no such thing.
There's an intent flag called FLAG_ACTIVITY_PREVIOUS_IS_TOP and the name implies something like that but I don't understand the description:
"If set and this intent is being used to launch a new activity from an
existing one, the current activity will not be counted as the top
activity for deciding whether the new intent should be delivered to
the top instead of starting a new one. The previous activity will be
used as the top, with the assumption being that the current activity
will finish itself immediately"
Could someone please decode that for me, and if it's not what I want IS there some way to tell an activity to submerge itself below the previous one?
This isn't possible. The activities are stacked and you cant put one back under the other. It sounds like you may want to create a class that extends Android’s android.app.Application.
I found this tutorial online and it looks good. Good luck.
Extending Android's android.app.Application tutorial
You cannot move an activity below a certain activity into the android back Stack. The only way to move a activity in back stack is to open another activity on top of it. You can move an activity on top by creating a single instance of activity using FLAG 'singleTop' in this way your activity will be moved to the top of another activity and only a single instance of activity will be there in stack.
More information about activity back stack and Flags is available here.
Go through this information and all your doubts will get cleared about back stack.
I have two activities A and B. The A has a ListFragment which uses LoaderManager, whereas B activity shows a details about the item selected in the A's ListFragment. I've just noticed that when I use a back button to get from the B back to the A, the position in the ListFragment preserve, but when I use the up button (left-point caret) in the action bar, the A activity is recreated and thus position in list view is lost.
I would like fix this issue, but I am not sure about the best way how to do it right.
I come up with this solutions:
a) Use onBackPressed()
Replace the default implementation for the android.R.id.home (the up action bar button) in the B activity, and instead of the NavUtils.navigateUpFromSameTask(this) function call the onBackPressed() activity method. I've tested it and it works.
b) Keep use NavUtils.navigateUpFromSameTask(this)
But implement the onSaveInstanceState and restore listView position during onCreate method of the ListFragment used by the A activity. (I've not tested this approach yet)
Which of this solutions is better? Or is there any other (much more better) solution?
Solution a) is pretty simple and straight forward, but b) is probably better because the default implementation of the up caret is used.
Any ideas are welcome. Thanks.
Solution c is the correct option. First, though, an explanation of the problem with solution a.
There is absolutely no point in having two back buttons in your Activity. Furthermore, option a actually breaks the up button. The point of the up button is to provide a way for users to stay within your app when they have landed in your app from an outside source. For example, if you land on activity B from an outside activity C and if you are using your option a, then pressing "up" in activity B will result in activity C being shown. The behavior you would want would be for activity A to be shown.
As you can see, solution b is on the right track. You definitely want to go up to A and not back to C. However, simply storing the state in onSaveInstanceState will not cause the state to be retained. This is because onSaveInstanceState only gets called if your application may be killed by the system. It is not guaranteed to be called if your application was destroyed manually, and it certainly won't be called when a new instance of your Activity is created. If the Intent starts a new activity, then it will not have its state restored from the other activity.
There solution, then, is that you must write anything persistent to a shared preference file (or a custom persistent alternative). When doing this you can guarantee that all instances of an Activity share the same state across multiple tasks so long as their onResume (or wherever you restore state) is called. OR:
If you know exactly how you want your navigation to work, you can avoid writing everything to persistent state by using a combination of Intent flags and Activity task affinities. If you want to use the same activity as up even if you navigate into the application from an outside source, then you can leave your Activity A's affinity as default (linked to the application) and use something like Intent.FLAG_ACTIVITY_CLEAR_TOP.
Personally, I'd try the Intent flag approach first and failing that fall back to writing the state persistently. You just don't really want scroll location sitting on persistent storage if you can avoid it..
check out this presentation: https://speakerdeck.com/jgilfelt/this-way-up-implementing-effective-navigation-on-android. It answers to all of your problems.
Ok . . . I have read hours upon hours of advice and forums and developer pages to no conclusive answer.
what I have: is an app with 8 activities and each has a slightly different set of buttons to navigate to some of the other activities. Each activity has multiple forms of data collection, ultimately ending in an e-mail intent which collects all application UI data.
Problem: I cant seem to find the proper way to use the navigation buttons in conjuction with onSavedInstanceState so that no matter what, everytime an activity is started it will appear with any and all data the user has inputed(sp?) thus far, during this instance of the application. I dont want to save data beyond closing the app. I do want the user to be able to navigate back and forth as much as necessary within the activities without losing data until the app closes.
This is what I have:
Activity A: has buttons to: Activity B, C, D, E, and F(F=email intent)
Activity B: has buttons to: Activity G, and A
Activity G: has buttons to: Activity B, and A
Activity C: has buttons to: Activity H, I, and A
Activity H: has buttons to: Activity C, I, and A
Activity I: has buttons to: Activity H, I, and A
Activity D: has button to: Activity A
Activity E: has button to: Activity A
Is there a better way to set up my navigation? (this is optimal for the context I am fairly sure)
And what is the proper launch mode/Flags/savedinstancestate and whatever to achieve proper result? I can explain elaborate as much as needed.
I am aware I need to save the data the problem is setting up the activities so that the navigation buttons i have put in each activity always open activity showing all previously saved data (in the current task instance)
I have all activites set to launchMode:singleTask, I have not manually coded any flagsand I have set up onsavedinstance properly but something as simple as: Activity B-> Activity A-> Activity B (using only my buttons) Activity B saves nothing after going to A and back again (again I am not using back key, I am using my navigation buttons in the layout) How can I fix this??
Oddly tho Activity A DOES retain memory but none of the other activities do
Why you're using singleTask?
Aren't you looking for
intent.setFlag(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
?