How can I pass data from one activity to another - android

I tried to look up something similar to what I want, but I can't find anything...
I want to pass data that I received from my database (echo json_encode($response); ) from one activity to another. The data is displayed in a listview and by clicking one item (in this case a 'category') it will take the user to second activity that displays a list of books (displayed in a listview).
My goal is to display results from received data (selected 'category' from first activity and selected 'book' from second activity) in a third activity (in here the user will have information about the book).
Any help will be great - a link to a tutorial that can help me solve this problem (well problem for me because I am new to all this), an example code or suggestions on how can I look it up...anything.
Thanks :)

You can use Intent for passing data from one activity to another activity. If you want to pass multiple types of data from one activity to another, Bundle will also be another choice for you. Look for tutorials regarding passing data through intent or bundle from one activity to another activity
Here is an Example to get you started
Intent i = new Intent(getApplicationContext(),toActivityName.class);
i.putExtra("Key","value");
startActivity(i);

Related

keep data retained of listview when switching between activities

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.

Saving data from Spinners, RadioGroups ,EditText, across several activities to re-use

Being very new to android I have limited experience despite having built several basic apps, so I would be grateful if you could structure any replies with this in mind.
I would like to: after a signup/login section, Collect user selections from spinners, radio buttons and text input over two or three Activities and in a fourth activity display the collected data so that it can be checked going back to make changes if need be.
When correct move on to a fifth activity where the data is structured and emailed to an email address given on the signup section. I have made the basic structure including Spinners with String arrays and have indeed made an app that works as required, passing data with extras to the next activity, however I would like to pass data to the final activity not just the next one in line. I do not really want to use a database if I can avoid it, but possibly so when all the data has been collected and reviewed, and having tried Sharedpreferences when things go wrong and the app crashes I struggle to find out why, so in an attempt to make things clearer I've stripped all that out back to basic structure leaving just working spinners, radiogroups and navigation buttons and here I am.
Being that the app structure I have left has 5 Activities and 5 class files I was hesitant to post the code as I was not sure that was the done thing, however I am willing to be guided on the question.
In your first activity, after collecting the data from the user, you can add them to your Intent using Intent.putExtras(Bundle). This is an example using a username that the user entered in an EditText.
String username = myEditText.getText().toString();
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle extras = new Bundle();
extras.putString("username",username);
intent.putExtras(extras);
startActivity(intent);
In the second activity, you can get the data from the extras in your onCreate like this:
Bundle extras = this.getIntent().getExtras();
String username = extras.getString("username");
When going to the third/fourth... activity, you can do the same steps to carry your data in the Intent you use to open the activity. First get the data from the extras, and then add them to the extras of the next intent.
You would probably want to make the variables global or because you will probably want to use them inside an onClick where they will not be accessible.

modifying the listview with the result returned bu the another activity in android

i am very confused about how i should implement the following in my android application.
Let me explain the functionality that i want :
-i have a list view in my First Activity , say Activity A
-now , from this activity , i start another activity(activity B) with an intent for a result.
-with the result i get from the B, i want to update the listview in activity A.
-That is add an item in listview with the string returned.
i am thinking of, storing the items in array list and setting the array list to an array adapter to the listview.
now when the Activity B returns with a result i modify the arraylist and again set it to the listview.
i want to know, is it possible??
and also i have a question : when does the activity B returns?
so that in activity A's which methods(such as onResume(), onStart(), onRestart() ) should i write the logic for modifying the listview...?
i am very new to android development
Regarding your first question, I think you are on right track. You can just add the returned string to an ArrayList and then to the ArrayAdapter.
For your second question, you will get a clear understanding how it works, by seeing the activity flow diagram here: http://developer.android.com/reference/android/app/Activity.html

how to retrieve the data from arraylist that is passed statically in android?

I am passing my array list data statically from one page to other page in android. For the first time when I executed the application I am getting the array list data statically to the second page. I kept a home button at the top in the second page. I clicked on the home button and started from 1st page again. This time I am not getting the arraylist data to the second page. I am clearing the arraylist in home button click action. But why is the data not coming for the second time. Please help me with this issue....I am struggling for this a lot....
Thanks in advance...
Use Iterator... without the use of iterator , u cannot implement multiple retrival....
When you clear the ArrayList, how will you get the data? Either don't clear it or pass a Bundle in the Intent to transfer the List.

what is the best way to transfer back and forth between 2 activities

My application has a main form activity-1 with some <EditTexts> and a spinner, based on spinner selection, need to display another activity-2 with more form fields (multiselectable <CheckBoxes>), after user submitting the 2nd activity, need to display the activity-1 to allow the user to fill remaining fields of activity-1 (if any).
Finally, need to submit all the values from activity-1,2 to a webservice.
Can anybody please let me know the best way to do this.
Thanks,
nehatha
From Activity-1 launch Activity-2 using startActivityForResult(intent). You can pass whatever info you want from Activity-1 to Activity-2 via intent extras using intent.putExtra() APIs. Then when Activity-2 is done, your Activity-1's onActivityResult() will be called.

Categories

Resources