Acticity 之间的切换


MainAcitivity:

package com.example.startactvityforresutl;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {

	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		final	Activity activity=this;
		Button button=(Button) findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(activity,ActivityB.class);
				intent.putExtra(Constants.MainActivity_KEY, "hello ! i am from MainActivity!");
				startActivityForResult(intent,Constants.request_code);
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		Log.d(this.getClass().getName()+" :onActivityResult","requestCode:"+requestCode+"  resultCode:"+resultCode);
		
		Log.d(this.getClass().getName()+" :onActivityResult",data.getStringExtra(Constants.ActivityB_KEY));
	}
}

AcitivityB:

package com.example.startactvityforresutl;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;


public class ActivityB extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_b);
		
		Intent intent=this.getIntent();
		String s=intent.getStringExtra(Constants.MainActivity_KEY);
		Log.d("ActivityB输出",s);
		
		final	Activity activity=this;
		Button button=(Button) findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(activity,MainActivity.class);
				intent.putExtra(Constants.ActivityB_KEY, "hello ! i am form Activtiy B !");
				
				activity.setResult(Constants.result_code, intent);
				
				activity.finish();
			}
		});
		
		
	}
}

Constants:

package com.example.startactvityforresutl;

public class Constants {

	public	static	final	String	MainActivity_KEY="key";
	
	public	static	final	String	ActivityB_KEY="key";
	
	public	static	final	int	request_code=9;
	public	static	final	int	result_code=10;
}



在Adroidmanifest添加活动:
Acticity 之间的切换_第1张图片


在两个活动中 各加一个按钮:



<RelativeLayout 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"
    tools:context="com.example.startactvityforresutl.MainActivity" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="返回MainActivity" />

</RelativeLayout>


你可能感兴趣的:(Activity,交替)