Iám new in android.
My questions about sub-activity.
Main activity with child sub-activity 1 and 2.
I have a Main activity with 3 buttons with these buttuns i pass values to my child activity number 2 and in child 2 i do something with these values.
When i push the button in child 2 i would return the values to child number 1 and put the values in TextViews.
Now my problem is when i finish child 2 the values return automaticaly to the Main activity.
This not what i want is there solution for this.
Were can i place startActivityForResult . I don't want to place it in the Main activity.
Thanks in advance.
I would add the values as extras to the Intent that you use to start child 1, just as you did when you passed values to child 2.
Related
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.
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.
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.
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.
What I want to do is relatively simple: I have 3 tables on my SQLite database: Filmes (Movies), Genero (Genre) and Filmes_Genero (a table that make a relationship between the tables). So I have a screen with a movie cadastre form and another screen that lists the possible genres for the movie that is called by a button from Cadastre screen. So how can I get the values of checked boxes back to my Cadastre Activity?
You start the activity using startActivityForResult() on your Cadastre activity, and then override the onActivityResult() method to read the result data (in a moment). In your checkbox activity, before you call finish(), you must set the extras on the activity result. This page has a simple example.