Setting Up a Second Activity

strings.xml中添加新的字符串

<?xml version="1.0" encoding="utf-8"?>
<resources>
  ...
  <string name="warning_text">Are you sure want to do this?</string>
  <string name="show_answer_button">Show Answer</string>
</resources>

Creating a new activity

创建一个activity需要用到至少三个文件:java class file, xml layout, application manifest。 为了避免出错最好使用Android Studio的向导创建

Android视图模式下,右键com.bignerdranch.android.geoquiz,选择New - Activity - Empty Activity。 在弹出的对话框中,Activity Name填写CheatActivity,点击完成
改写activity_cheat.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center"
              tools:context=".CheatActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:text="@string/warning_text"/>
    <TextView
        android:id="@+id/answerTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        tools:text="Answer"/>
    <Button
        android:id="@+id/showAnswerButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/show_answer_button"/>
</LinearLayout>

Declaring activity in the manifest

manifest文件位于app/manifests目录下,文件名为AndroidManifest.xml

Adding a cheat button to QuizActivity

activity_quiz.xml中添加button

<Button
  android:id="@+id/cheat_button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/cheat_button"/>

QuizActivity.java中关联button

private Button mCheatButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
  mCheatButton = (Button)findViewById(R.id.cheat_button);
});

Start an Activity

public void startActivity(Intent intent)

该函数是系统的一个函数,更精确地说是ActivityManager

Communicating with intent

component通过intent和系统通信。根据不同的通信要求Intent类有不同的构造函数。目前我们需要使用的构造函数为:

public Intent(Context packageContext, Class<?> cls)

Class<?>参数即为需要让ActivityManager启动的activityContext参数为包含该activitypackage
QuizActivity.java中,mCheatButton点击事件

mCheatButton.setOnClickListener(new View.OnClickListener(){
  @Override
  public void onClick(View v){
    Intent i = new Intent(QuizActivity.this, CheatActivity.class);
    startActivity(i);
  }
})

在启动activity前,ActivityManager会线在manifest中查找要启动的activity的名字。

Passing Data Between Activities

QuizActivity需要告诉CheatActivity当前问题的答案,CheatActivity需要告诉QuizActivity用户是否作弊

Using intent extras

Intentextra可以传递任意数据,采用键值对的形式。

CheatActivity.java

public static Intent newIntent(Context packageContext, boolean answerIsTrue){
    Intent i = new Intent(packageContext, CheatActivity.class);
    i.putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue);
    return i;
}

传递QuizActivity.java

mCheatButton.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        // start cheatActivity
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
        Intent i = CheatActivity.newIntent(QuizActivity.this, answerIsTrue);
        startActivityForResult(i, REQUEST_CODE_CHEAT);
    }
});

获取CheatActivity.java

mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);

显示答案CheatActivity.java

mAnswerTextView = (TextView) findViewById(R.id.answerTextView);
mShowAnswer = (Button)findViewById(R.id.showAnswerButton);
mShowAnswer.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        if(mAnswerIsTrue){
            mAnswerTextView.setText(R.string.true_button);
        }else {
            mAnswerTextView.setText(R.string.false_button);
        }
        setAnswerShowResult(true);
    }
});

Getting a result back from a child activity

public void startActivityForResult(Intent intent, int requestCode)

requestCode是用户自定义的一个整数,传给child activity后会再次被传回。 当启动多个child activity时可以用来区分是那个传回的。

child activity中通过调用

public final void setResult(int resultCode);
public final void setResult(int resultCode, Intent data);

来设定返回值

private void setAnswerShowResult(boolean isAnswerShown){
    Intent data = new Intent();
    data.putExtra(EXTRA_ANSWER_SHOW, isAnswerShown);
    setResult(RESULT_OK, data);
}

当在CheatActivity返回键时,ActivityManager会自动调用QuizActivity中的函数:

protected void onActivityResult(int requestCode, int resultCode, Intent data);

来获取返回值

CheatActivity.java中添加函数以便于被获取

public static boolean wasAnswerShown(Intent result) {
    return result.getBooleanExtra(EXTRA_ANSWER_SHOWN, false);
}

Handling a result

添加变量保存返回结果

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(resultCode != Activity.RESULT_OK){
        return;
    }

    if(requestCode == REQUEST_CODE_CHEAT){
        if(data == null){
            return;
        }
        mIsCheater = CheatActivity.wasAnswerShow(data);
    }
}