Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swdev:android:start [2018/05/04 06:10]
smayr [Explicit Intent]
swdev:android:start [2018/05/22 14:15] (current)
smayr [Resources]
Line 5: Line 5:
     * [[https://developer.android.com/index.html|Android Development Resources]]     * [[https://developer.android.com/index.html|Android Development Resources]]
     * [[https://developer.android.com/studio/index.html|Android Studio (IDE)]]     * [[https://developer.android.com/studio/index.html|Android Studio (IDE)]]
 +    * [[https://flutter.io/get-started/codelab/|Flutter (IDE)]]
     * [[https://www.visualstudio.com|Visual Studio (IDE) + Xamarin]] - Select //Community Edition//     * [[https://www.visualstudio.com|Visual Studio (IDE) + Xamarin]] - Select //Community Edition//
  
Line 172: Line 173:
                 intentDataHandler.putExtra("Key2", "Value2");  // data payload (optional)                 intentDataHandler.putExtra("Key2", "Value2");  // data payload (optional)
                 intentDataHandler.putExtra("com.acme.myapp.Key3", "Value3");  // use full path with key (recommended)                 intentDataHandler.putExtra("com.acme.myapp.Key3", "Value3");  // use full path with key (recommended)
 +                intentDataHandler.putExtra("Key4.TEAM_OBJ", aTeamObj); // parcelable object (eg. object of type Team)
                 startActivity(intentDataHandler);                 startActivity(intentDataHandler);
                 break;                 break;
Line 209: Line 211:
             str = getIntent().getExtras().getString("Key3");             str = getIntent().getExtras().getString("Key3");
             Toast.makeText(this, str, Toast.LENGTH_SHORT).show();             Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
 +        }
 +        
 +        // Parcelable data
 +        Team objTeam;
 +        if (intent.hasExtra("Key4.TEAM_OBJ")) {
 +            Log.d("MyApp", "Retrieving intent extras...");
 +            objTeam = intent.getParcelableExtra("Key4.TEAM_OBJ");
 +            Toast.makeText(this, "Team Name: "+ objTeam.name, Toast.LENGTH_SHORT).show();
         }         }
     }     }
Line 214: Line 224:
 </code> </code>
  
 +A parcelable extra must be of a type supporting parcelable.  For example:
 +<code java>
 +package com.acme.myapp;
 +
 +import android.os.Parcel;
 +import android.os.Parcelable;
 +
 +public class Team implements Parcelable
 +{
 +    public long id = 0;
 +    public String name = "";
 +    public String number = "";
 +    public int ranking = 0;
 +
 +    // Default constructor
 +    Team()
 +    {
 +
 +    }
 +
 +    // In constructor, read variables from Parcel. 
 +    // Important: Read them in the same sequence in which they were written in Parcel.
 +    public Team(Parcel in) {
 +        id = in.readLong();
 +        name = in.readString();
 +        number = in.readString();
 +        ranking = in.readInt();
 +    }
 +
 +    @Override
 +    public int describeContents() {
 +        return 0;
 +    }
 +
 +    // Write member variables in Parcel. 
 +    // Write in any order. Not required to write all members in Parcel.
 +    @Override
 +    public void writeToParcel(Parcel dest, int flags) {
 +        // Write data in any order
 +        dest.writeLong(id);
 +        dest.writeString(name);
 +        dest.writeString(number);
 +        dest.writeInt(ranking);
 +    }
 +
 +    // De-serialize the object
 +    public static final Parcelable.Creator<Team> CREATOR = new Parcelable.Creator<Team>(){
 +        public Team createFromParcel(Parcel in) {
 +            return new Team(in);
 +        }
 +
 +        public Team[] newArray(int size) {
 +            return new Team[size];
 +        }
 +    };
 +}
 +</code>
 === Intent Examples === === Intent Examples ===
 For example, in the ''MainActivity'', you can define an intent: For example, in the ''MainActivity'', you can define an intent: