Passing data through intent using Serializable

前端 未结 11 1509
借酒劲吻你
借酒劲吻你 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

    I extended ρяσѕρєя K's answer to make the code full and workable. So, when you finish filling your 'all_thumbs' list, you should put its content one by one into the bundle and then into the intent:

    Bundle bundle = new Bundle();
    
    for (int i = 0; i<all_thumbs.size(); i++)
    bundle.putSerializable("extras"+i, all_thumbs.get(i));
    
    intent.putExtras(bundle);
    

    In order to get the extras from the intent, you need:

    Bundle bundle = new Bundle();
    List<Thumbnail> thumbnailObjects = new ArrayList<Thumbnail>();
    
    // collect your Thumbnail objects
    for (String key : bundle.keySet()) {
    thumbnailObjects.add((Thumbnail) bundle.getSerializable(key));
    }
    
    // for example, in order to get a value of the 3-rd object you need to:
    String label = thumbnailObjects.get(2).get_label();
    

    Advantage of Serializable is its simplicity. However, I would recommend you to consider using Parcelable method when you need transfer many data, because Parcelable is specifically designed for Android and it is more efficient than Serializable. You can create Parcelable class using:

    1. an online tool - parcelabler
    2. a plugin for Android Studion - Android Parcelable code generator
    0 讨论(0)
  • 2020-11-22 13:56

    Sending Data:

    First make your serializable data by implement Serializable to your data class

    public class YourDataClass implements Serializable {
    String someText="Some text";
    }
    

    Then put it into intent

    YourDataClass yourDataClass=new YourDataClass();
    Intent intent = new Intent(getApplicationContext(),ReceivingActivity.class);
    intent.putExtra("value",yourDataClass);
    startActivity(intent);
    

    Receiving Data:

    YourDataClass yourDataClass=(YourDataClass)getIntent().getSerializableExtra("value");
    
    0 讨论(0)
  • 2020-11-22 14:00

    You need to create a Bundle and then use putSerializable:

    List<Thumbnail> all_thumbs = new ArrayList<Thumbnail>();
    all_thumbs.add(new Thumbnail(string,bitmap));
    Intent intent = new Intent(getApplicationContext(),SomeClass.class);
    
    Bundle extras = new Bundle();
    
    extras.putSerializable("value",all_thumbs);
    intent.putExtras(extras);
    
    0 讨论(0)
  • 2020-11-22 14:01

    Create your custom object and implement Serializable. Next, you can use intent.putExtra("package.name.example", <your-serializable-object>).

    In the second activity, you read it using getIntent().getSerializableExtra("package.name.example")

    Otherwise, follow this and this page.

    0 讨论(0)
  • 2020-11-22 14:03

    In kotlin: Object class implements Serializable:

    class MyClass: Serializable {
    //members
    }
    

    At the point where the object sending:

    val fragment = UserDetailFragment()
    val bundle = Bundle()
    bundle.putSerializable("key", myObject)
    fragment.arguments = bundle
    

    At the fragment, where we want to get our object:

    val bundle: Bundle? = arguments
        bundle?.let {
            val myObject = it.getSerializable("key") as MyClass
            myObject.memberName?.let { it1 -> showShortToast(it1) }
        }
    
    0 讨论(0)
提交回复
热议问题