In my app I need to ask the user a quick input
.
I need to get a result from this Flash-Activity and then get back to the previous one.
I've read about the StartActivityForResult()
method, but I'm not yet sure how to use it properly, any examples?
EDIT:
I've tried to pass the Player object via intent (as the result) using the method I used in all the app applied to this method of the StartActivityForResult():
In my second Activity (the one where I need to get the result from):
Intent intent = new Intent();
Player playerKilled = players.get(position);
Bundle bundle = new Bundle();
bundle.putSerializable("PLAYER_KILLED", (Serializable) playerKilled);
intent.putExtras(bundle);
setResult(Activity.RESULT_OK, intent);
finish();
My Main Activity where I need to take the result to:
if (resultCode == Activity.RESULT_OK) {
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
playerKilled = (Player)bundle.getSerializable("PLAYER_KILLED");
Toast.makeText(this, playerKilled.getName() + "the " + playerKilled.getCardName() + " has died, and he/she had the ID: " + playerKilled.getId(), Toast.LENGTH_SHORT).show();
If I understand your question correctly, I am confident you want this solution:
simply create your activity with a list of items (recyclerview?) then to start it in order to get the results back, you basically create your intent and call the above method.
Now, after you are in the list activity, simply do the following when a user has selected the item:
So, immediately a user clicks an item from the list, you use this code to pass back your value and close this list activity.
Lastly, to actually receive this value passed, you simply Override
onActivityResult()
This is all you need! I would say creating an activity is totally fine even though it does just one thing - displays a list of items! I hope this helps you! Good luck and happy coding!
You can request user input in a number of ways, but if you want to use a new
Activity
, as you mentioned, we can usestartActivityForResult()
to launch a new activity and return the input from there.Firstly, I would highly recommend reading through this Stack Overflow answer about how to use
startActivityForResult()
. I will explain how you can implement it for your specific use case.So, you need to understand that
startActivityForResult()
has two parameters:Intent
(which you use to pass data between activities)It is good practice to use a constant for your request code that you can access in both of your activities. For example, in your main activity, you could add:
This would be accessible in both activities since it is
public
, and it would work for a request code since it is anint
(integer).In your main activity, you need an action (such as a button press) to start your second activity. Let's assume it is a button click that triggers this action:
Basically, we are creating an
Intent
which specifies our current activity (this
) and the second activity. We use the intent in thestartActivityForResult()
method along with theREQUEST_CODE
we declared earlier.Now, in our second activity, we need something to trigger the data being returned back. From the previous question you asked, I'm assuming you want data to be returned to the main activity when a
RecyclerView
item has been clicked. Here is part of my answer to that question modified to show how data will be sent back.The above will send back the position of the list item from the
RecyclerView
clicked.Have a look at the
Intent
'sputExtra()
method. This is what passes data. You can see that I have passed a String"pos"
and the variable for the itemposition
in this method, but why:The
putExtra
method ofIntent
s always use a String key and another object, such as your integer variable,position
. The key will be used again when retrieving this object (as I will show you later).We use
Activity.RESULT_OK
to say that we are passing a result, but you can useActivity.RESULT_CANCELED
if you don't want to send back a result - this is explained in the answer link I mentioned at the beginning. Note that if you useActivity.RESULT_CANCELED
, you would not need to useputExtra
as there would be nothing to send back.Finally, you need to add something into your main activity which will deal with receiving the results from your second activity:
We use the
onActivityResult
method for this.At the beginning of this method, we are checking to see if the
requestCode
is the same as the one we defined earlier (as a public constant,REQUEST_CODE
).If so, we continue and check to see what the
resultCode
of the result was. If data was sent back (Activity.RESULT_OK
), we can retrieve it usinggetIntExtra()
as theposition
was an integer (similarly, usegetStringExtra()
if you are returning a String). Then you can do something with the returned data. However, if data was not sent back (as we mentioned earlier withActivity.RESULT_CANCELED
), you can do something else.Hopefully this helps you with implementing your idea, but a Google search would've found the answer I mentioned above (here is the link again) which clearly explained how to use
startActivityForResult()
. Other answers for this question also explain it well, but perhaps you needed guidance on how to implement it in your use case (i.e. combined with your code from the previous question you had), which is why I've provided the explanation above.You can also read the Android documentation on Getting a Result from an Activity as well as Android documentation for the [
startActivityForResult()
method](https://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)) andIntent
s.What you can do is pop up an activity that looks like a dialog.
In the manifest, where you declare the activity, include a dialog theme:
Then call the activity the normal way you would, with a result if you like: