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.
Related
I have two activities one is saved address activity and the other is a cart activity both have a button to navigate to the same page which is map to get the location and after finding the location their redirected to another page which is to fill their house no and after filling them it has a button when clicked I want an event to get back to their starting activities
I used intent extras to put information as key values 1 and 2 in the corresponding pages and checked it at the end but still no use
If I have understood correctly you want to send the information (location, house no) back to your starting activity?
One "dirty" solution would be like:
Intent mIntent = new Intent(mContext, YourDestinationActivity.class);
mIntent.putExtra("location", location);
mIntent.putExtra("houseNo", houseNo);
startActivity(mIntent);
But for this kind of workflow I would use Fragments
A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).
I have hybrid app where every page is loaded inside a WebPageActivity(webView). I don't want to create a new activity for every screen but create multiple instances of same activity for each hybrid html page.
Following is the requirement,
Navigation Stack:
A -> B -> D -> E
Here all activities in stack are of type WebPageActivity and every instance is drawing different html.
When user clicks on some button on activity 'E', then it should bring existing 'B' to foreground and clear top, resulting in following stack,
A-> B
Summary:
All the activities in the stack are of same type but having different views and it is required to go back to some activity in stack with clear top.
Available data:
Every activity holds property identifying the name of html file.
Whenever I want to back to activity in the stack, I know the name of html that will be present in that activity.
You should use a Fragment and not make a new instance of the same activity just for displaying a different html. Android has Fragments for the very same use-case as yours.
Also, creating multiple activity instances increases your app's memory footprint. Using Fragments you can easily remove any Fragment from the Fragment back stack by using its unique fragment tag (which you specify while adding it to the activity).
In order to achieve it by making multiple activity instances, make use of the flag CLEAR_TOP in the intent for activity instance 'B'. This should clear all instance on top of B.
Hope this helps
I have a MainActivity with a container FrameLayout in which I change multiple Fragments (Fragment A, Fragment B etc).
In one of this fragments let's say Fragment A I have to open another activity (Activity X).
The problem is that from this activity when I press a button I have to change Fragment A with Fragment B (in the background somehow) and after that, slideout Activity X (with translate animation), and slidein Fragment B ,all this without restarting the MainActivity because I have to keep the state.
How can I do this?
Thanks
Android uses loosely coupled components as its main building blocks. As you know, Activities are one of the main Android building blocks. Thus, interacting between activities are very restricted to a few ways.
Passing data via Intents by startActivity(), startActivityForResult() etc. This way is useful whenever you are starting new activities.
Sending broadcast Intents. This could be useful once you want to send a signal to your another app's component.
Utilizing shared Application object.
Java static fields and some other ways.
In your case I would recommend you to use a Dialog Fragment instead of your second activity, if your second activity is just a login activity or something like that.
UPDATE #1:
If you really would like to keep your second activity, so I would personally recommend using local broadcast mechanism.
Also there are another way to get this done. You could start your second activity as startActivityForResult and then whenever user gets back from your second activity to your first one, your first activity can get informed by its onActivityResult method. There you could switch those fragments.
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.
Its possible to retain a Fragment between Activities?
Lets say I have Activity A with Fragment F_Left placed at the left and Fragment F_Right placed at the right. If I want to launch a new Activity and keep Fragment F_Left... how can I do it?
Can I retain Fragment F_Left state between activities?
Note that I want to launch a new Activity because Fragment F_Left is my app menu and Fragment F_Right changes completely the context of the user operations... and my app have many of operations, so it makes sense to have an Activity per operation.
I know its possible to retain Fragment within an Activity, but as Fragment life cycle is closely tied to the container Activity I don't know if this is possible keep Fragment state between Activities.
Since API Level 13 (HONEYCOMB_MR2, June 2011), you can save and restore the state of a fragment across activities.
To save the state, use FragmentManager.saveFragmentInstanceState(), providing a reference to the Fragment whose state you wish to save. The Fragment must be attached at the time you attempt to save its state.
To restore the state, use Fragment.setInitialSavedState() with the return value when you instenciate the same Fragment.
myFragment = new MyFragment();
myFragment.setInitialSavedState(appState.getMyFragmentState());
fragmentManager.beginTransaction().add(R.id.container, myFragment).commit();
You can persist the SavedState object across activities as you would any other object; one way is to subclass Application as shown above (appState is the instance of our subclass).
Based on your response to my comment, I have a slightly different answer. It may not end up being the best answer in your specific situation, I'll let you decide that. :)
Right now you are bundling your fragments in activities because that is what made sense to you, but really, you can probably treat the entire process as one activity and use fragment transactions to hide & show (or create and destroy) fragments as needed.
Since you won't be creating and destroying activities, your menu fragment on the left will be left untouched, and you won't have any problems with its UI state. The set of operations you want to run (which no doubt includes all sorts of different fragments on the right) does not need to be launched in a new activity - but you will have to find a way to manage the logic you need for the fragment transactions (either in your one über-activity or in some kind of OperationsManager class).
I think this will end up being a lot smoother for the users of your application since the single activity just remains running - and you are only changing the parts that actually need to change.
If I want to launch a new Activity and keep Fragment F_Left... how can I do it?
Don't launch a new activity.
Can I retain Fragment F_Left state between activities?
Not automatically. It is not the same fragment. You would pass data between the activities for use by the fragment no differently than you would without any fragments at all.
To potentially answer your original question, if you fire off another activity then I believe that you can save your fragment from your first activity by calling FragmentManager::putFragment(...) when onSaveInstanceState(...) is called and then getting it back later, e.g. in onCreate(...).
However, I have to agree with Mark D's response.
Incidentally I'm doing something similar in that I have a dual pane setup whereby the left pane if fixed with a number of options with each option invoking a different fragment in the right pane. Furthermore selecting an entry in the right pane can result in the right fragment being replaced by another one.
However, I have taken the approach whereby by left fragment is only responsible for displaying and handling responses from the immediate fragment which appears in the right hand pane. Furthermore each right-hand fragment is then responsible for 'replacing' itself with a new fragment and handling results sent back to it. I'm using setTargetFragment, getTargetFragment, and calling onto the target fragment's onActivityResult method to pass results back.
For me the approach I've taken is no different from when my app runs on a phone with a single pane whereby the initial option's activity only knows about the activies it fires off and subsequently these new ones fire off further activies which they know about.
It should be mentioned that my activity in my dual pane app doesn't really do much apart from loading the left pane fragment and I can't quite see the need for a single activity to ever have to manage hundreds of fragments.