android the method by which intent passes list or an object

  • 2020-06-15 10:10:50
  • OfStack

This article illustrates how intent passes list, or objects, in android. Share to everybody for everybody reference. Specific implementation methods are as follows:

Method 1:

If you just pass List < String > Or List < Integer > You can use it directly

The code is as follows:

intent.putStringArrayListExtra(name, value)  
intent.putIntegerArrayListExtra(name, value)

Method 2:

If List is passed < Object > , you can convert list strong to Serializable type, and then pass

putExtras(key, (Serializable)list)

The method is passed on and used when receiving
(List<YourObject>) getIntent().getSerializable(key)

You can get List < YourObject > The data

But keep in mind that your YourObject class must implement the Serializable interface

Method 3:

1 kind is

Bundle.putSerializable(Key,Object);

The other one is
Bundle.putParcelable(Key, Object);

Of course, these Object are conditional on the former implementing the Serializable interface and the latter implementing the Parcelable interface

Method 4:

We can write one global data in application

1. Create your own subclass of ES65en.app.Application
2. Declare the class under 1 in manifest,
3. android creates a globally available instance for this purpose. You can use the method Context.getApplicationContext () anywhere else to get the instance and thus the state (variable).

Inheritance Application

The code is as follows:

class MyApp extends Application {  
    private String myState; 
    public String getState(){ 
    return myState; 
  } 
  public void setState(String s){ 
    myState = s; 
  } 
}

As for the configuration in ES83en.ES84en, it was originally only necessary to add name to application, as shown below:

<application android:name=".MyApp" android:icon="@drawable/icon"  android:label="@string/app_name">

use

class Blah extends Activity {  
    @Override 
  public void onCreate(Bundle b){ 
    ... 
    MyApp appState = ((MyApp)getApplicationContext()); 
    String state = appState.getState(); 
    ... 
  } 
}

I hope this article has been helpful for your Android programming.


Related articles: