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
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 three top level activities in my application. Activity A, B & C.
Each one of these activities hosts a navigation drawer. I am trying to figure out the best way to manage the activity stack between these three activities.
For example, When I start the application, Activity A is launched.
Activity A has a navigation drawer like Activities B & C. When I click on Activity B in the drawer, Activity B is launched and clicking on Activity C in the drawer launches Activity C etc...
I don't want to finish these Activies when the drawer launches a new Activity because they load data from a backend service, and when I click the back button I want it to send the application to the background.
Essentially, I am looking for a way to launch the activity if it does not exist, and if it does, just resume it. How can I accomplish this?
I think decoupling retrieving data from the activity is the best option.
The following paragraph is from Tasks and Back Stack:
Because the activities in the back stack are never rearranged, if your application allows users to start a particular activity from more than one activity, a new instance of that activity is created and pushed onto the stack (rather than bringing any previous instance of the activity to the top). As such, one activity in your application might be instantiated multiple times (even from different tasks), as shown in figure 3. As such, if the user navigates backward using the Back button, each instance of the activity is revealed in the order they were opened (each with their own UI state).
So in your case, grabbing the data in the background when the app starts using async tasks and storing them in the database might work out better.
One way to do it would be:
On create of the home activity, quickly grab the home activity's data via async task while showing a progress bar. When done, store it, and display it. Then, launch async tasks for the data for other activities. There are some conditions that could be tricky. For example, you have to make sure you show a progress bar if the user quickly switches to Activity B or C before your data is ready.
Perhaps using a singleton might suite your needs if you do not want to use the DB. Depending on the size of your data, parceling your data and passing it through a bundle might also prove to be a good technique.
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 currently have an Android application when in the process of browsing a hierarchy of objects creates a back stack like this.
BrowseActivity(Starting Instance) -> BrowseActivity(Instance B) -> BrowseActivity(Instance C) -> ViewObjectActivity
There is a menu button in the view object activity that I would like to have take the user back to BrowseActivity(Starting Instance) and destroy the rest of the browse activities.
Using Intent.FLAG_ACTIVITY_CLEAR_TOP only destroys the ViewObjectActivity and leaves the rest in the back stack.
Setting the BrowseActivity to "singleTop" in the manifest breaks the ability to create another instance for further navigation.
Any way to accomplish this that I am probably overlooking?
If you want the menu option to take the user back to the first activity, do just that - take the user back to the first activity.
Have your third activity exit with a specific result that tells the second activity to exit as well. You can extend this to how many activities you want - as long as all the activities except the first one exit, you're good.
You need to use this flag in a pair. The following has always worked for me
Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent.FLAG_ACTIVITY_NEW_TASK
lets imagine this application like enclosed image. R stands for Root activity and A and B are other activities. A is for displaying of some list, B stands for displaying detail of some value. From R I can get by 4 buttons to 4 A activities. My question is, whether in this scenario is A activity initialized for each time or I would be using only one A activity among whole application. If user selects A in right top corner activity A, then displays detail B and then from detail B goes to another list (but based on A activity). Will he still got the same content from the first A or he can have "new" A activity with another list?
Here is the point - I will be having let's say hundred of activites. Lot of them are "forms" displaying some content, application will have about 50 variants of those forms. Can I make for each form one activity a reuse it again in my activity without having connections to past usage of this activity?
Use android:launchMode="singleInstance" in your manifest if you want a single instance of your activity class
By default activities start over entirely every time you open them with a call to startActivity(). If your activity starts a new activity, it will essentially be put on to an activity stack and is paused while the new activity starts. If the user presses the back button, the last activity on the stack will resume as it left off (though I don't think this is 100% guaranteed as Android will kill off tasks as it needs resources so I wouldn't assume it).
So if you have this chain here:
A->B->C->D
where each letter represents a new activity with absolutely no flags or changes.
If the user is at D and presses the back button, C will resume. D is popped from the stack. If the user decides to go to activity D again, a new D will start as if it didn't happen (assuming you didn't save any persistent variables). If the user presses back twice, the application will be at B where C and D are no gone.
You can manipulate this chain with various flags like singleInstance to keep its state, or noHistory to ensure it doesn't get put to the stack (meaning it will be skipped if the user presses back).
Very detailed description of the various attributes