Activity之间通过Intent传递数据
Intent intent = new Intent(this, TwoActivity.class);
intent.putExtra("key", "value");
startActivity(intent);
像这样直接通过Intent put就可以传递一些基本被类型的数据,并在第二个Activity中通过key去获取相应的值
getIntent().getStringExtra("key");
Bitmap类,实现了Parcelable借口,所以如果想要传递Bitmap对象则可以直接传递过去
public void onClick(View view) {
Intent intent = new Intent(this, TwoActivity.class);
intent.putExtra("key", "value");
Bitmap bitmap = BitmapFactory.
decodeResource(getResources(), R.mipmap.ic_launcher);
intent.putExtra("bitmap", bitmap);
startActivity(intent);
}
在第二Activity中调用getParcelableExtra方法接收:
((ImageView) findViewById(R.id.image))
.setImageBitmap((Bitmap) getIntent().getParcelableExtra("bitmap"));
如果想要传递实体类,则需要该实体类实现Serializable接口
package wkk.app6;
import java.io.Serializable;
/**
* Created by wkk on 2016/7/19.
*/
public class Test implements Serializable {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
传递
Test test=new Test();
test.setMessage("123");
intent.putExtra("test",test);
startActivity(intent);
接收
Intent intent = getIntent();
Test test = (Test) intent.getSerializableExtra("test");
有时候我们需要在第二个Activity中做了一些操作后返回上一个Activity的时候将数据返回给上一个Activity,那么我们就需要调用
startActivityForResult(intent,requestCode);
方法参数是Intent和requestCode
在第二个Activity中调用setResult方法返回:
private static final int resultCode = 2;
private void back() {
setResult(resultCode, new Intent());
finish();
}
并且在第一个Activity中通过重写onActivityResult方法接收数据:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// 请求码 返回码
if(requestCode==this.requestCode&&resultCode==TwoActivity.resultCode){
//data就是上一个Activity调用setResult方法时传递过来的Intent
Toast.makeText(this,data.getStringExtra("a"),Toast.LENGTH_SHORT).show();
}
}
Activity向Fragment传递数据:
想要传递的数据可以放在bundle中
FragmentA a = new FragmentA();
Bundle bundle = new Bundle();
a.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(R.id.frameLayout, a).commit();
Fragment获取传递过来的数据:
Bundle bundle = getArguments();
Fragment向Activity传递消息有两种方法
第一个是,通过getActivity()方法获取到相应的Actvivity对象,然后调用Activity中的方法
MainActivity mainActivity = (MainActivity) getActivity();
mainActivity.showToast("呵呵");
第二个就是通过接口回调了,在Fragment内写一个接口让Activity去实现,并在需要的时候调用,可以将数据以参数的形势传递过去
package wkk.app5;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentA a = new FragmentA();
a.setOnListener(new FragmentA.onListener() {
@Override
public void doSomeThing(String messgae) {
//做一些需要做的事情
Toast.makeText(MainActivity.this,messgae,Toast.LENGTH_SHORT).show();
}
});
Bundle bundle = new Bundle();
a.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(R.id.frameLayout, a).commit();
}
public void showToast(String message){
Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
}
}
Fragment
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
/**
* Created by Think on 2016/7/19.
*/
public class FragmentA extends Fragment implements View.OnClickListener {
private Button tiaozhuan;
private View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("测试", "onCreateView");
view = inflater.inflate(R.layout.fragment_a, null);
tiaozhuan = (Button) view.findViewById(R.id.tiaozhuan);
tiaozhuan.setOnClickListener(this);
Bundle bundle = getArguments();
MainActivity mainActivity = (MainActivity) getActivity();
mainActivity.showToast("呵呵");
if (onListener != null) {
onListener.doSomeThing("监听到一些事情");
}
return view;
}
private onListener onListener;
public void setOnListener(FragmentA.onListener onListener) {
this.onListener = onListener;
}
public interface onListener {
void doSomeThing(String messgae);
}
}
Fragment切换时传递消息:
同样可以通过setArguments方法传递消息,这里就不多说了
那么如果想要在一个Fragment返回上一个Fragment时将数据返回回去
可以通过setTargetFragment方法来实现
FragmentA
FragmentB b = new FragmentB();
b.setTargetFragment(this, 123);
getFragmentManager().beginTransaction().hide(this)
.add(R.id.frameLayout, b, "Fragment")
.addToBackStack(null).commit();
FragmentB
Intent intent=new Intent();
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
intent.putExtra("bitmap", bitmap);
intent.putExtra("a","你好");
getTargetFragment().onActivityResult(123,123,intent);
getFragmentManager().popBackStack();
FragmentB通过getTargetFragment方法来得到上一个Fragment传递过来的FragmentA的对象来调用FragmentA的onActivityResult()方法,将数据放在Intent中返回
接收的方法和Activity类似:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 123 && requestCode == 123) {
String a = data.getStringExtra("a");
((EditText) view.findViewById(R.id.aet)).setText(a);
((ImageView) view.findViewById(R.id.image)).setImageBitmap((Bitmap) data.getParcelableExtra("bitmap"));
}
}
完整代码下载地址:
http://download.csdn.net/detail/w18756901575/9580339