In my app, I create the gridview and then add the view by RelativeLayout LayoutParams.
But how can i call it in another activity?
such as gridview looping the 10 article and hide the icon, i create the edit button to control hide/show the icon. but i dont know how to call it.
Thanks.
Once you switch activities, I don't think you can directly access the ui elements of the previous activity. You should try to send data between activities by creating intents with arguments like this :
Start an Activity with a parameter
If you from activity A to B and you want to pass data back to A, launch A with an intent that has the extra data you need.
Parse the data from the intent and update your UI accordingly.
Related
I am having 2 activities, in each activity I am having a view-flipper and button. When the button in 1st activity is clicked, I want to view the 2nd activity's view-flipper child. How to do that? Is that Possible? If yes, please tell me how to do that.
Thanks in advance.
You cannot directly interact with views between activities. Instead, you should pass data in an Intent when you call startActivity() from the first activity. The data should tell the second activity which view to start with. For example, you can send the name or index of the view. You can learn more about sending data in an Intent from the official Android documentation.
i am struggling to keep data or items of list view when i leave one activity to another, for my example i created simple app on click will add number increased to a list view so each click create an item like 1 another click add 2 and so on.
the program works fin for main activity but then i would like to see the result on another activity i call it second activity but the problem is when i hit back button on second activity to go back to main activity, i will lose all of items on the list view.
i was looking on google so many information but could not get my head around it, so please advice and please be little more in detail as i am beginner, i think instance state or shared preference will do the job but i do not know any of them
thanks in advance and here is my app code for main activity and second activity and picture for output
sorry i add code as images becausethe site keep saying the code need indentation thank you
main activity[main out put][2]second activity[second activity out put][4]
You need to save the data of the ListView in some form, either in a file or in a database (local or remote).There is no direct way to store list view, but you can store the data from the list view and then set it in to the ListView later when you switch back to the activity.
You need to keep in mind that switching activity results to invoking of onPause() method in android, and if the data is not saved in the current activity then it will be lost when you move on to another activity.
Add all your values into the array, pass it to the adapter, then after clicking on the list view item, make an intent where you want to switch your activity (means from one activity to second activity), while implemented the intent, pass your array also, using intent.put extra. Then you will get your array in your second activity and coming back to your previous activity, again pass an intent (with your array) and get your array in your previous activity and pass it into the adapter.
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.
In android suppose we intent an activity2 from activity1..so how can we use or get the ids of elements of activity2(such as relative layout etc) in activity1..and code in activity 1 using the element of activity 2
If you are trying to change the UI of Activity1 based on Activity 2 then you should start it as child activity and use onActivityResult.
If you are trying to access some values of Activity1 in Activity 2 then you can pass them via bundle.
I have a button on the main activity that when clicked runs a method. In the method I have an intent to a second activity, but i want to set the content view of that activity with the button from the first activity because i want to have more than one button on the first activity, but reuse the second activity and just change the layout. So....
Click button1 > sets view to layout1 > starts activity with layout1 as the contentview
OR
Click button2 > sets view to layout2 > starts activity with layout2 as the contentview
I would like to do it this way to avoid creating too many activities
Thanks
there are many ways to do it. here are just a few of them:
pass the argument of which layout to use by adding extra int to the intent (putExtra) and on the onCreate of the second activity use the intent to get it.
use fragments instead of activities , there you would have even more ways to do it.
not recommended - using a static int.
Add extra info to your intent using Intent.putExtra(String key, int data). Then in the second activity use getIntent().getIntExtra(key). You can use something other than an integer for your data but I recommend it so you can easily use a switch block.
EDIT:
Also, as yarian said:
You can just pass the layout itself, it's just an int that sits in the R file.
It is probably a good idea to do this to eliminate the switch block (unless you need to execute other code as well, but this is still a good idea as you won't be defining separate constants for each layout to pass) So in your first activity say:
intent.putExtra("LAYOUT", R.layout.layout_name);
And in the second:
setContentView(getIntent().getIntExtra("LAYOUT"), DEFAULTVALUE);
Hope I helped!
When either of the buttons is clicked you start the second activity through an intent, in which you put value 1 if the first button is clicked, value 2 if the second one is clicked.
Then in the second activity you read a value from an Intent and if it's 1 you setContentView to be the first layout, if it's 2 to be the second layout.