Cohort+Problem+2+-+Activity+Lifecycle,+Intents

=Part 1 - Create Two Layouts=

What the app should look like
The objective of this cohort problem is to show you how your app can communicate between two activities. The outcome of this Example is shown below.



activity_main.xml
We stick with LinearLayout and specify its orientation as vertical. Here are the xml tags for the widgets. I hardcode the strings here for clarity.

code format="xml" 













 

code

Create a new activity
Right click on the app folder icon, New->Activity->Empty Activity.

Suppose you name this **NextActivity**. Then edit the associated XML layout file (**activity_next.xml**). If it doesn't have one, put in a TextView widget and give it an id.

=Part 2 - Write the callback for the button =

Code skeleton
One callback method can take care of the following scenarios. I leave you to decide how you want to implement the callback. In Cohort Problem 1, you learnt three ways of doing it. Which ever way you choose, let's assume that your method signature looks like this:

code format="java" @override public void onClick(View v){

//TO DO 1 - write code to check if password matches boolean isPasswordMatch = ;

//TO DO 2 - write code to get the context

if( isPasswordMatch ){

//TO DO 3 - password matches, write the code for the intent

}else{

//TO DO 4 - password does not match, write the code for whatever you'd like }

} code

TO DO 1 - write code to check if password matches
get the text from the password widget and compare it to the required password. I leave you to do this.

TO DO 2 - write code to get the Context (Explaining what happens)
The **Context** class is an abstract class several levels above in the inheritance hierachy of **AppCompatActivity** class. It stores information about the environment that the Activity resides in. The documentation gives details and as usual, stack overflow usually has a good explanation.

The View v that is passed to the callback is an instance of the button that is clicked.

The following code thus gets the "context" in which the button resides in.

The intent requires an instance of the context class.

code format="java" Context context = v.getContext; code

An Intent brings the user from an Activity to some other part of the phone.

If it is to another Activity, then we are using an **Explicit Intent.**

We'll show you how to build an Explicit Intent (because the documentations for Intents is very dense ... ).

Typically, data in Android is stored as a set of key-value pairs.

Hence, set up a static String variable as a key somewhere in your class definition. This is the key for the next activity to access your message. code format="java" public static final String KEY = " "; //text as you wish. usually the package name is included. code

We are ready for the code recipe to make the intent. We specify our intent to move from MainActivity to NextActivity. Using the putExtra method, we then store the user's name as a message together with a key. We then launch the intent and the next activity is displayed. The message is also passed to the next activity. code format="java" Intent intent = new Intent(context, NextActivity.class); String message = name.getText.toString; intent.putExtra(KEY,message); startActivity(intent); code

Explaining what is NextActivity.class
In the following line, put the cursor on the constructor call and press Alt+Space. The definition will pop up.

code format="java" Intent intent = new Intent(context, NextActivity.class); code From the definition, the second argument requires an instance of a **Class** object. The Class object (with a capital C) gives you data about what any particular object contains.

TO DO 4 - password does not match, up to you to decide what happens
display a Toast or an AlertDialog displaying a suitable message. Try doing it yourself.

Get the Intent in NextActivity.java
Within the onCreate method, You receive the intent and retrieve the message. Set the message as part of the text of the TextView widget and put in any frills you'd like.

code format="java" Intent intent = getIntent; String message = intent.getStringExtra(MainActivity.KEY);

TextView welcome = (TextView) findViewById(R.id.welcomename); welcome.setText("Welcome, " + message); code Anything else you want to put in this activity is up to you. I put a picture of pikachu ...

Specifying the parent activity in the android manifest
You can use the back button on the phone to go back to MainActivity.

However, by specifying that **MainActivity** is the parent of **NextActivity**, your app gains an extra button.

The part of your Android Manifest (app/manifests) should look like this: code format="xml"  code Observe what happens in your NextActivity screen.

=Part 3 - Activity Lifecycle=

This app can also help you see the Android Activity Lifecycle.

Referring to the flowchart, there are seven lifecycle callbacks, and we've already implemented **onCreate**.

Implement the other six that you see in the flow chart. You can use the log class (please read the link!) for this purpose. For example:

code format="java" @Override public void onResume { super.onResume; // Always call the superclass method first Log.i(TAG,"onResume is invoked"); } code

See which methods are invoked as you do the following
 * change the orientation of the phone
 * launch NextActivity
 * get back to MainActivity from NextActivity

Do they tally with the story told in the flowchart?

=Part 4 - Exploring Widgets - RadioGroup and RadioButton=

Lastly, let's use this chance to see how radio buttons and a radio group work together.

To help you avoid needless google-searching for obscure information, most of the callback to show and hide the password is here.

code format="java" RadioGroup showhidepassword = (RadioGroup) findViewById(R.id.showhidepassword);

EditText password = (EditText) findViewById(R.id.passwordfield);

showhidepassword.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener {           @Override            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {

switch(checkedId) {                   case R.id.showpassword: password.setTransformationMethod(null); break; case R.id.hidepassword: password.setTransformationMethod(PasswordTransformationMethod.getInstance); break;

}           }        });

code The learning point for you here is that there is a different listener method for each kind of widget action.

This is also an example where it is not possible to specify the callback in the XML tags, unlike a button.

=Part 5 - Layout Weights=

In **NextActivity.xml,** add 2 or 3 widgets as you like and include the layout_weight attribute in each one of them. Experiment with the values and how the sizes of the widget turn out, both in portrait and landscape mode. Read the LinearLayout documentation for more information.

code format="xml"  code