Android_Intent Demo(acticity1与activity2相互跳转)

Main.activity:

package com.example.intentdemo2;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btn;
    private static final int REQUEST_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button) findViewById(R.id.myButton1);
btn.setOnClickListener(new OnClickListener(){

public void onClick(View arg0){
Intent intent=new Intent();
intent.setClass(MainActivity.this, secondActivity.class);
intent.putExtra("str", "com from first activity");
startActivityForResult(intent,REQUEST_CODE);

}
});
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_CODE){
if(resultCode==secondActivity.RESULT_CODE){
Bundle bundle=data.getExtras();
String str = bundle.getString("back");
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();

}

}

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}


secondActivity:

package com.example.intentdemo2;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
public class secondActivity extends Activity {
private TextView tv;
private Button btn2;
public static final int RESULT_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
String str=bundle.getString("str");
tv=(TextView) findViewById(R.id.myTextView1);
tv.setText(str);
btn2=(Button) findViewById(R.id.myButton2);
btn2.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent intent=new Intent();
intent.putExtra("back", "come from second activity");
setResult(RESULT_CODE,intent);
                finish();
}
});
}
}

main.xml:

<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" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="第一个 activity" />
    <Button
        android:id="@+id/myButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="first"/>
</LinearLayout>


second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >    
    <TextView
        android:id="@+id/myTextView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="第二个 activity" />
    <Button
        android:id="@+id/myButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="second"/>
</LinearLayout>


在 AndroidManifest.xml中添加 <activity android:name=".secondActivity" android:label="Second Activity"></activity>

你可能感兴趣的:(Android_Intent Demo(acticity1与activity2相互跳转))