[英]Pass an Parcelable Object from an Fragment to an activity
我有一個片段MyFragment
public class MyFragment extends Fragment {
//Some code here like to constructor
//Trying to pass an object to another Activity
Intent i = new Intent(getActivity(), NextActivity.class);
startActivity(i);
i.putExtra("test", parcableObject);
getActivity().finish();
}
我有一個活動NextActivty
public class NextActivity extends AppCompatActivity {
//Some code here like to constructor
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next_activity);
Intent intent = getIntent();
mBoard = intent.getParcelableExtra("test");
Log.d(TAG, "onCreate: " + mBoard);
}
這是我的Board
班
public class Board implements Parcelable {
//Implements all the Parcelable methods.
protected Board(Parcel in) {
//Auto-generated code here
}
public static final Creator<Board> CREATOR = new Creator<Board>() {
//Auto-generated code here
}
@Override
public int describeContents() {
//Auto-generated code here
}
@Override
public void writeToParcel(Parcel dest, int flags) {
//Auto-generated code here
}
}
我的mBoard始終為null
。
我在這里有沒有做的事情嗎?
你把多余的東西放錯了順序。 開始活動前先放好它。
public class MyFragment extends Fragment {
//Some code here like to constructor
//Trying to pass an object to another Activity
Intent i = new Intent(getActivity(), NextActivity.class);
i.putExtra("test", parcableObject);
startActivity(i);
getActivity().finish();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.