I am trying to pass a stack from one activity to another in android studio.
Currently I have a stack called: Stack<Integer> teamAstack = new Stack<>();
And I am passing it like this:
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,GameReview.class);
intent.putExtra("stack1",teamAstack);
startActivity(intent);
}
From what I have looked online to pass objects I need to do it like this?
Receiving Activity
getIntent().getSerializableExtra("stack1");
I initially tried setting it equal to a new stack which I made in the new activity but it says the types are different, with one being a stack and the other being serializable. I am unsure what do to to pass the stack correctly so that I can start popping and pushing from the new activity, thanks for all your help! There was nothing specifically for stacks on all my searches which is why I am having this issue.
New Activity:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Stack;
public class GameReview extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_review);
Stack<Integer> stack11 = (Stack<Integer>)getIntent().getSerializableExtra("stack1");
Stack<Integer> stack22 = (Stack<Integer>)getIntent().getSerializableExtra("stack2");
}
}
when you call getIntent().getSerializableExtra("stack1");
you get an object of type Serializable
since you know this object is of type Stack<Integer>
you can cast it and store it in a variable
Stack<Integer> stack = (Stack<Integer>)getIntent().getSerializableExtra("stack1");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.