Bundle傳送資料

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button button=new Button(this);
        //把View加到畫面上
        setContentView(button);
        //設定button監聽事件
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //實例化一個Intent物件
                Intent intent = new Intent();
                //設定要start的Avtivity,第一個參數是現在的Activity,第二個參數是要開啟的Activity
                intent.setClass(MainActivity.this, NewActivity.class);
                //實例化一個Bundle物件
                Bundle bundle = new Bundle();
                //設定要傳的資料,第一個參數是key,第二個是傳輸的資料
                bundle.putString("key", "send successful");
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
public class NewActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textView=new TextView(this);
        setContentView(textView);
        //get傳送過來的Bundle
        Bundle bundle=getIntent().getExtras();
        //以key取得資料
        String string=bundle.getString("key");
        //設定TextView內的文字
        textView.setText(string);
    }
}