/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.explicitintent;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
/* Fields that will store our EditText and Button */
private EditText mNameEntry;
private Button mDoSomethingCoolButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* Using findViewById, we get a reference to our Button from xml. This allows us to
* do things like set the onClickListener which determines what happens when the button
* is clicked.
*/
mDoSomethingCoolButton = (Button) findViewById(R.id.b_do_something_cool);
mNameEntry = (EditText) findViewById(R.id.et_text_entry);
/* Setting an OnClickListener allows us to do something when this button is clicked. */
mDoSomethingCoolButton.setOnClickListener(new OnClickListener() {
/**
* The onClick method is triggered when this button (mDoSomethingCoolButton) is clicked.
*
* @param v The view that is clicked. In this case, it's mDoSomethingCoolButton.
*/
@Override
public void onClick(View v) {
/*
* Storing the Context in a variable in this case is redundant since we could have
* just used "this" or "MainActivity.this" in the method call below. However, we
* wanted to demonstrate what parameter we were using "MainActivity.this" for as
* clear as possible.
*/
Context context = MainActivity.this;
// COMPLETED (1) Store ChildActivity.class in a Class object called destinationActivity
/* This is the class that we want to start (and open) when the button is clicked. */
Class destinationActivity = ChildActivity.class;
// COMPLETED (2) Create an Intent to start ChildActivity
/*
* Here, we create the Intent that will start the Activity we specified above in
* the destinationActivity variable. The constructor for an Intent also requires a
* context, which we stored in the variable named "context".
*/
Intent startChildActivityIntent = new Intent(context, destinationActivity);
// COMPLETED (3) Replace the Toast with code to start ChildActivity
/*
* Once the Intent has been created, we can use Activity's method, "startActivity"
* to start the ChildActivity.
*/
startActivity(startChildActivityIntent);
}
});
}
}