Skip to content

Intent Extra Binding

Steven Lewi edited this page Jul 13, 2015 · 4 revisions

@IntentExtra can be used to bind Activity's Extra. It has same functionality as getIntent().get...Extra("key");

First Activity
@ContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity {

    @Override
    protected void onContentViewCreated() {
    }

    @OnClick(R.id.button1)
    public void onButton1Clicked() {
        Intent i = new Intent(this, SecondActivity.class);
        i.putExtra("id", 1);
        i.putExtra("full_name", "John Doe");
        i.putExtra("user_profile", new Profile());
        startActivity(i);
    }
}
Serializeable Class that can be passed thru Intent Extra
public class Profile implements Serializeable {
    public String name;
    public String address;
}
Second Activity
@ContentView(R.layout.activity_second)
public class SecondActivity extends BaseActivity {

    @IntentExtra("id")
    private int id;

    @IntentExtra("full_name")
    private String fullName;

    @IntentExtra("user_profile")
    private Profile userProfile;

    @Override
    protected void onContentViewCreated() {
         Toast.makeText(this, fullName, Toast.LENGTH_SHORT).show();
    }
}

For fragment arguments/parameters, take a look into this.