My understanding is that best practices for a simple app with list and detail Activities would be a startActivityForResult(). Let's say I have an app with model Product (id, name, price) and ProductListActivity and ProductDetailActivity.
I am having a hard time understanding which of the activities would contain startActivityForResult() and which would have setResult() and what the result would be.
My understanding is that best practices for a simple app with list and detail >Activities would be a startActivityForResult().
I would not agree with that statement.
In my opinion, the best way would be to set up a [onListItemClickListener](http://developer.android.com/reference/android/app/ListActivity.html#onListItemClick(android.widget.ListView, android.view.View, int, long)) in your ProductListActivity.
When an item in the list is clicked, the onListItemClick() method will be called, from the arguments you'll be able to figure out exactly which item in the list was clicked (for example using the second argument - position, which is the position of the clicked item in the list, starting from zero).
Once you figure out which item was clicked, you'll create an Intent and put the required extras in there (for example the position of the clicked item).
You'll then use that intent to start your ProductDetailActivity, by calling startActivity().
Then, in your ProductDetailActivity you'll get that intent using getIntent(), extract the extras from that intent and then use them to decide what to display in your activity (for example, display some text associated with a position in your listview from the ProductListActivity). You can get extras from an intent using getExtra*() methods.
You would use startActivityForResult() if you wanted to get some result back from that activity, which in your example doesn't seem to be the case, you simply want to display some details. Now imagine that your activity did some calculations and you wanted to get the final value back, then using startActivityForResult() would be one of the ways of passing that value back.
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.
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);
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
I'm a very new to Java. I thought I was doing okay but have now hit a brick wall, so to speak.
The idea is I use the 'home button' '_menu' for the user to choose one of 26 formats. The format they choose has 3 variables. Those 3 variables are then used in the first xml view, along with 2 user inputs to calulate another variable. This variable is then used in a second xml view, along with a third user input here, to calculate the final answer.
I have created the 26 choices and if for example I choose option 5, on the emulator screen I see all the correct values associated with this choice. I don't think those values are getting stored anywhere. I say this because if I come out of that view and return back into it, it's not showing, as in my example, choice 5. It's showing its initial state, as though I was going into it the first time. I assume it's something to do with launching this activity from the start but is there anyway around this. Or really where do I start.
My second question is with the integer variables that I created from this choice. I need to pass them into another java file for the first set of calculations. I've tried to pass the variables/data with the, 'new intent putExtra' but can't get it to work. But I don't think I can use this anyway since the I don't want to launch the second view directly from the res/menu/ .xml view.
Not sure if this is making sense to anyone. Can someone help point me in the right direction?
I don't quite understand your first question, but it sounds like you're launching a new activity and when you quit and come back to it, everything is reset. If you're launching a new activity after selecting the options from your phone's menu button, you should implement a method that saves data to the shared preferences of the main activity. This method should be called on the activities onPause(), onDestroyed(), or onStop() method. You can also add a method on onResume() where the activity checks if there's any data stored in shared preferences and if so, make the desired changes.
As for your second question...I kinda don't understand it either. new intent and putextra is used when you're starting a new activity and want to pass data to it. Views are not "launched" they are only inflated and brought on display whenever you want. I did an app once where I had everything in one activity and just using setContentView() method all the time. In the long run, it just complicated everything. It is easier to keep things simple and launch activities. Here is an example of some variables being passed to a new activity:
On my main activity (FirstActivity) I have:
(when newActivityButton is clicked)
case R.id.newActivityButton:
Intent mIntent = new Intent(FirstActivity.this,SecondActivity.class);
String[] luckyNumbers = {
luckyNumber[0].getText().toString(),
luckyNumber[1].getText().toString(),
luckyNumber[2].getText().toString(),
luckyNumber[3].getText().toString(),
luckyNumber[4].getText().toString(),
luckyNumber[5].getText().toString()};
mIntent.putExtra("luckyNumbers", luckyNumbers);
mIntent.putExtra("message", messageField.getText().toString());
FirstActivity.this.startActivity(mIntent);
break;
luckyNumbers[] is an array of textviews.
Then on my NewActivity onCreate(), I have:
message = getIntent().getExtras().getString("message");
Log.i("TAG", message);
luckyNumbers = getIntent().getExtras().getStringArray("luckyNumbers");
cv.setLuckyNumbers(this.luckyNumbers);
cv.setMessage(this.message);
where cv is just a custom view I created with its own methods
I have a question on the Android Activity, for example I have a TabHost, and there are included four Activities, the first tab is a search Activity, enter a keyword in the current result of this Activity to return, and in the current Activity display. Is called to display the search results themselves. And after searching several times, and then return to key mobile phone keypad, the display is the result of the last search keyword, I want the press back key to return to the last call of the Activity or TabHost. Should I do?
By the way, in a tab in the use of Intent calls a Activity,
eg: host.addTab (host.newTabSpec ("friend"). setIndicator ("search")
. SetContent (new Intent (this, Search.class)));
In this Activity in the need to call another Activity,
e.g: startActivity (new Intent (this, Other.class));
Also called another Activity displayed on this tab, but not yet jump out of the show. I ask how you can achieve this?
First, you can use startActivityForResult(...) instead of startActivity(...). This means that when you're done with the activity, you want to come back to the activity that started it so you can do more.
Second, you can override the onKeyDown(...) method and define whatever behavior you want for the back key. however, this is not recommended by Google except when absolutely necessary.