01_Activity生命周期及传递数据

1. Activity的生命周期:

01_Activity生命周期及传递数据

2. Activity启动另一个Activity,并传递数据:

package com.example.activitydemo;



import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;



public class MainActivity extends Activity {



    protected static final String EXTRA_MESSAGE = "com.example.activitydemo.MESSAGE";

    public Button btnActivityChange;

    public EditText editMessage;

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        btnActivityChange = (Button) findViewById(R.id.btn1);

        editMessage = (EditText) findViewById(R.id.editMessage);



        btnActivityChange.setOnClickListener(new View.OnClickListener() {



            @Override

            public void onClick(View v) {

 Intent i = new Intent(MainActivity.this, Activity2.class);

                

                String message = editMessage.getText().toString(); i.putExtra(EXTRA_MESSAGE, message); startActivity(i);

            }

        });



    }



}
package com.example.activitydemo;



import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;



public class Activity2 extends Activity {



    private Button btnBack;

    private TextView textView1;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity2);



        btnBack = (Button) findViewById(R.id.btnBack);

        textView1 = (TextView) findViewById(R.id.textView1);



 Intent intent = getIntent(); textView1.setText(intent.getStringExtra(MainActivity.EXTRA_MESSAGE));



        btnBack.setOnClickListener(new View.OnClickListener() {



            @Override

            public void onClick(View v) {

                finish();

            }

        });



    }



}

 

你可能感兴趣的:(Activity)