Sunday, August 2, 2015

Second Activity and Intents

We can use Android Studio's New Android Activity wizard to create an activity called ThingActivity.

Then let us update onClick method of the ThingViewHolder to start this new activity.

MainActivity.java
public void onClick(View view) {
    Intent intent = new Intent(MainActivity.this,ThingActivity.class);
    intent.putExtra("id",  name.getText());
    startActivity(intent);
}

Update the activity's XML and onCreate method.

activity_thing.xml
<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

ThingActivity.java
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_thing);
    String id = getIntent().getStringExtra("id");
    ((TextView)findViewById(R.id.name)).setText(id);
}

Then there is a bit of work to use the toolbar, e.g., enabling the Toolbar (aka, Action Bar).  There is quite a bit here involving updating activity_thing.xml, ThingActivity.java, and AndroidManifest.xml.  Also got rid of the code for the menu including: menu_thing.xml. Also All done right, one will have a navigation arrow leading back up to the parent activity.  Details on this are at: https://developer.android.com/guide/topics/ui/actionbar.html.

No comments:

Post a Comment