I'm developing a simple home launcher. Main activity is the home layout, in it there is a button to call another activity to show the apps list. In this second activity, if you long press an icon, it adds to home screen.
So, from the second activity (apps list) I add an imageview to the main activity (home screen), but to do this I need to get the main layout, which it's not possible from different activity than itself.
To achieve this, I'm thinking different options like this:
1-Declaring a variable in the second activity:
public static RelativeLayout mainLayout;
Set it in the first:
// MainActivity
Intent i = new Intent(this, DrawerActivity.class);
DrawerActivity.mainLayout = (RelativeLayout)findViewById(R.id.mainlayout);
startActivity(i);
2-Using fragments avoiding the jumps between activities.
3-Changing the layout in the main activity (one activity for two layouts).
4-Declaring class descending from Application and store here everything I need in the different activities.
5-Use of broadcasters.
The question is: which is the right approach to achieve this ?
I've read several docs but there is no clear answer.
Definitely don't go with option 1. That's the best way to leak memory and context, and you don't want that. Resources associated with an Activity (in this case the layout) should be private to that Activity. This ensures that the framework can manage memory as it was designed to.
To communicate between activities, the official way is to pass parameters in the intent that launches the activity. You can add the identifier of the application you want to add to the main screen, and than retrieve the image either from the intent (as seen in the link) or if second Activity is launched for a result, then in the onActivityResult method of your home Activity.
However in you case I would suggest another approach. As you have to persist the layout of the home screen, I would create a database table containing the positions of the applications added to the main screen. I would modify the database entries where it's convenient and rebuild the main screen's layout every time it is displayed based on the database.
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 3 activities and want it to be possible to fill in a form on the activity after a 1 button takes me to the second activity that calls for his third activity. In the third activity I choose a value and want to go directly to the 1st activity but want the 1st activity is in the state it was in when it comes out.
I have this in 3th activity:
Intent selectFavorite = new Intent(view.getContext(), FirstsActivty.class);
selectFavorite.putExtra("Data", Info);
selectFavorite.putExtra("SUB", favoriteListArray.getJSONObject(position).getString("sub"));
selectFavorite.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
When i return the fill is blank :s
I am 99% there is a different and more reliable way to accomplish what you desire. If by any chance the reason you are going to SecondActivity and ThirdActivity is to get some input values for the FirstActivity's form, then you should consider doing this not in a different Activity, but using dialogs or similar. That way you will avoid having to unnecessarily manage more activity's lifecycle overridden method than what you actually need.
If you agree with me and you don't know if you should use dialogs, or action bar menu items or similar for what you want to accomplish, try checking out the Android design guidelines: http://developer.android.com/design/building-blocks/dialogs.html
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.
How do you handle multiple screens in an android application? I have developed with the tab bar at the bottom without problem, however what I am wanting to do is replace all the content on the screen with the content from a new .xml layout file I have created in the project. Additionally, how would I tie the back end code to the new layout file? I'm sure this question probably exists already and is googleable (may have made up a new word). However, I don't know exactly what it is that I am looking for. Thanks in advance for your help.
What you need to do is, create a new Activity and add it to the AndroidManifest.xml:
<activity android:name="ActivityClassName" android:label="Label for the Activity"></activity>
and can be called in a method:
public void startActivity() {
Intent someName = new Intent(CurrentClass.this, ActivityClassName.class);
startActivity(someName);
}
Android applications generally use a separate Activity for each screen, and switch between them using Activity.startActivity and Activity.startActivityForResult. You can pass arbitrary data to an Activity via Intent.putExtra.
Hope this helps,
Phil Lello
It really depends on how you want your application to flow.
Let's consider the scenario where a user does the following:
Starts your first activity
Presses the 2nd tab
Presses the 3rd tab
Presses the back button
If you use a separate activity for each screen, then the following would happen
Activity 1 is started
Activity 2 is started
Activity 3 is started
Activity 3 is closed, user returns to Activity 2
(in this case pressing the back button again would you take you back to Activity 1, and pressing it again would exit your application)
If you used one activity for all the tabs, then the following would occur
Activity 1 is started
Activity 1 sets tab content to tab 2 content
Activity 1 sets tab content to tab 3 content
Activity 1 is closed, user returns to home screen
If you are using a screen with tabs, then the second method (a single Activity with a TabHost or similar) is the preferred method, otherwise the user will end up making a large activity-stack just switching between tabs (meaning if they switch between tabs a lot they'll have to press the back button a lot of times to exit).
If you want to go for the single activity approach, then do some research on TabHost and TabContentFactory. In the createTabContent method of your factory you can inflate a View/layout from XML to set as the tab content using View.inflate. Look those up and come back ask another question if you get stuck ;)
i think you may want to play with more than one activity.... you can have multiple activities and one xml for each of them... in this way you can have different screens... check these links. Multiple Activities, Creating an Activity.... hope this helps...