/* * 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) { // COMPLETED (1) Retrieve the text from the EditText and store it in a variable /* We'll first get the text entered by the user in the EditText */ String textEntered = mNameEntry.getText().toString(); /* * 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; /* This is the class that we want to start (and open) when the button is clicked. */ Class destinationActivity = ChildActivity.class; /* * 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 (2) Use the putExtra method to put the String from the EditText in the Intent /* * We use the putExtra method of the Intent class to pass some extra stuff to the * Activity that we are starting. Generally, this data is quite simple, such as * a String or a number. However, there are ways to pass more complex objects. */ startChildActivityIntent.putExtra(Intent.EXTRA_TEXT, textEntered); /* * Once the Intent has been created, we can use Activity's method, "startActivity" * to start the ChildActivity. */ startActivity(startChildActivityIntent); } }); } }