How to put a List in intent
You have to instantiate the List
to a concrete type first. List
itself is an interface.
If you implement the Parcelable
interface in your object then you can use the putParcelableArrayListExtra()
method to add it to the Intent
.
put list of objects in `Intent`
It is not recommended to pass a list of objects between your activities. As @IlyaMaier said, a more robust way is to initialize your ViewModel with your application context and then reuse the same LiveData in your MainActivity.
if you want to continue the same way, there are two reasons why your MainActivity is not being called.
- The observer is not being able to receive new data in your ViewModel Class.
- The data class should extend Pacelable & also add annotation @Parcelize.
Like this:
@Parcelize
data class Channels (
var Id: Int?,
var Title: String?,
var CallLetter: String?,
var ChannelPosition: Int?
) : Parcelable
Intent.putExtra List
Assuming that your List is a list of strings make data an ArrayList<String>
and use intent.putStringArrayListExtra("data", data)
Here is a skeleton of the code you need:
Declare List
private List<String> test;
Init List at appropriate place
test = new ArrayList<String>();
and add data as appropriate to
test
.Pass to intent as follows:
Intent intent = getIntent();
intent.putStringArrayListExtra("test", (ArrayList<String>) test);Retrieve data as follows:
ArrayList<String> test = getIntent().getStringArrayListExtra("test");
How can I pass ListString as an Intent extra?
This is used to send using intent from one activity.
Intent intent = getIntent();
intent.putStringArrayListExtra("votedBy",new ArrayList<>(votedBy));
To receive this can be used;
ArrayList<String> votedByList= getIntent().getStringArrayListExtra("votedBy");
intent.putExtra a list of Parcelable objects and enums
You don't need to make List parcelable. To put it, just use existing method.
final Intent i = new Intent(this,SecondActivity.class);
final ArrayList<Product> list = new ArrayList< Product >();
i.putParcelableArrayListExtra(PRODUCT_KEY, list);
startActivity(i);
To retrieve almost in the same way.
final ArrayList<Product> list = this.getIntent()
.getParcelableArrayListExtra(PRODUCT_KEY);
UPDATE
To answer you newer question. You cannot pass generic List
inside. Because by List
you could have different collection. To know system, which type to use you need to have explicit type.
// This will work.
final ArrayList<Department> departments = getSpecificDepartments();
intent.putParcelableArrayListExtra(DEPARTMENTS_KEY, departments);
How to pass ArrayList of Objects from one to another activity using Intent in android?
It works well,
public class Question implements Serializable {
private int[] operands;
private int[] choices;
private int userAnswerIndex;
public Question(int[] operands, int[] choices) {
this.operands = operands;
this.choices = choices;
this.userAnswerIndex = -1;
}
public int[] getChoices() {
return choices;
}
public void setChoices(int[] choices) {
this.choices = choices;
}
public int[] getOperands() {
return operands;
}
public void setOperands(int[] operands) {
this.operands = operands;
}
public int getUserAnswerIndex() {
return userAnswerIndex;
}
public void setUserAnswerIndex(int userAnswerIndex) {
this.userAnswerIndex = userAnswerIndex;
}
public int getAnswer() {
int answer = 0;
for (int operand : operands) {
answer += operand;
}
return answer;
}
public boolean isCorrect() {
return getAnswer() == choices[this.userAnswerIndex];
}
public boolean hasAnswered() {
return userAnswerIndex != -1;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
// Question
builder.append("Question: ");
for(int operand : operands) {
builder.append(String.format("%d ", operand));
}
builder.append(System.getProperty("line.separator"));
// Choices
int answer = getAnswer();
for (int choice : choices) {
if (choice == answer) {
builder.append(String.format("%d (A) ", choice));
} else {
builder.append(String.format("%d ", choice));
}
}
return builder.toString();
}
}
In your Source Activity, use this :
List<Question> mQuestionList = new ArrayList<Question>;
mQuestionsList = QuestionBank.getQuestions();
mQuestionList.add(new Question(ops1, choices1));
Intent intent = new Intent(SourceActivity.this, TargetActivity.class);
intent.putExtra("QuestionListExtra", ArrayList<Question>mQuestionList);
In your Target Activity, use this :
List<Question> questions = new ArrayList<Question>();
questions = (ArrayList<Question>)getIntent().getSerializableExtra("QuestionListExtra");
Passing a List from one Activity to another
You can pass an ArrayList<E>
the same way, if the E
type is Serializable
.
You would call the putExtra (String name, Serializable value)
of Intent
to store, and getSerializableExtra (String name)
for retrieval.
Example:
ArrayList<String> myList = new ArrayList<String>();
intent.putExtra("mylist", myList);
In the other Activity:
ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");
Please note that serialization can cause performance issues: it takes time, and a lot of objects will be allocated (and thus, have to be garbage collected).
Related Topics
Force a Camera to Always Open in Portrait Mode in Android
Properly Using Asynctask Get()
How to Solve Nullpointerexception Error in Android
How to Import Contacts from Phonebook to Our Application
Android Java.Util.Concurrent.Timeunit Convert Milliseconds to Minutes
Android: How Handle Message Error from the Server Using Volley
Want to Compile Native Android Binary I Can Run in Terminal on the Phone
Android Screen Sizes in Pixels for Ldpi, Mdpi, Hpdi
The Key Must Be an Application-Specific Resource Id
Live Character Count for Edittext
Hardcoded String "Row Three", Should Use @String Resource
Why So Complex to Set Style from Code in Android
Loading All the Images from Gallery into the Application in Android
What Does the Filter Parameter to Createscaledbitmap Do
Sliding Navigation Drawer Not Handling Clicks on Menu Items Android
When Should One Use Rxjava Observable and When Simple Callback on Android