Passing data through intent using Serializable

前端 未结 11 1520
借酒劲吻你
借酒劲吻你 2020-11-22 13:35

I\'ve implemented my class with serializable, but it still didn\'t work.

This is my class:

package com.ursabyte.thumbnail;

import java.io.Serializab         


        
11条回答
  •  心在旅途
    2020-11-22 13:56

    This code may help you:

    public class EN implements Serializable {
    //... you don't need implement any methods when you implements Serializable
    }
    

    Putting data. Create new Activity with extra:

    EN enumb = new EN();
    Intent intent = new Intent(getActivity(), NewActivity.class);
    intent.putExtra("en", enumb); //second param is Serializable
    startActivity(intent);
    

    Obtaining data from new activity:

    public class NewActivity extends Activity {
    
        private EN en;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            try {
                super.onCreate(savedInstanceState);
    
                Bundle extras = getIntent().getExtras();
                if (extras != null) {
                    en = (EN)getIntent().getSerializableExtra("en"); //Obtaining data 
                }
    //...
    

提交回复
热议问题