喝酒不骑马的Android自学日记(13)-Fragment生命周期&数据传递***2016年1月11日
Fragment的生命周期
每一个Activity生命周期状态至少对应一个Fragment的生存周期状态
onAttach():当Fragment被添加到Activity时会回调这个反法,并且这个方法只会被调用一次
onCreate():创建Fragment时会回调,只会调用一次
onActivityCreate():当Fragment所在的Activity启动完成后,调用这个方法
onCreatView():每次创建都会绘制Fragment的View组件,此时回调这个方法
onStart():启动Fragment
onResume():恢复Fragment时被回调。调用onStart()方法后,一定会调用onResume()。
onPause():暂停Fragment
onStop():停止Fragment
onDestroyView():销毁Fragment所包含的View组件
onDestroy():销毁Fragment时被回调
onDetach():Fragment从Activity中删除时被回调,只能调用一次。
Fragment与Activity通信
Fragment可调用getActivity()方法获取它所在的Activity
Activity可调用FragmentManager的findFragmentById()ndFragmentByTag()方法获取Fragment。
Activity--->Fragment:在Activity中创建Bundle数据包,并调用Fragment的setArguments(Bundle bundle)方法。
Fragment--->Activity:需要在Fragment中定义一个内部回调接口,再让包含该Fragment的Activity实现该回调接口。这样Fragment可调用该回调方法数据传递给Activity。
从Activity向Fragment传送数据
通过Bundle数据包的形式,传递数据。
Bundle bundle = new Bundle();
bundle.putString("name", text);
fragment5.setArguments(bundle);
Code
MainActivity3
MyFragment5
2016年1月12日
----------------------------------------------------
Fragment的生命周期
每一个Activity生命周期状态至少对应一个Fragment的生存周期状态
onAttach():当Fragment被添加到Activity时会回调这个反法,并且这个方法只会被调用一次
onCreate():创建Fragment时会回调,只会调用一次
onActivityCreate():当Fragment所在的Activity启动完成后,调用这个方法
onCreatView():每次创建都会绘制Fragment的View组件,此时回调这个方法
onStart():启动Fragment
onResume():恢复Fragment时被回调。调用onStart()方法后,一定会调用onResume()。
onPause():暂停Fragment
onStop():停止Fragment
onDestroyView():销毁Fragment所包含的View组件
onDestroy():销毁Fragment时被回调
onDetach():Fragment从Activity中删除时被回调,只能调用一次。
Fragment与Activity通信
Fragment可调用getActivity()方法获取它所在的Activity
Activity可调用FragmentManager的findFragmentById()ndFragmentByTag()方法获取Fragment。
Activity--->Fragment:在Activity中创建Bundle数据包,并调用Fragment的setArguments(Bundle bundle)方法。
Fragment--->Activity:需要在Fragment中定义一个内部回调接口,再让包含该Fragment的Activity实现该回调接口。这样Fragment可调用该回调方法数据传递给Activity。
从Activity向Fragment传送数据
通过Bundle数据包的形式,传递数据。
Bundle bundle = new Bundle();
bundle.putString("name", text);
fragment5.setArguments(bundle);
Code
MainActivity3
package com.example.martin.fragment;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* Created by Martin on 2016/1/11.
*/
public class MainActivity3 extends Activity {
private EditText edittext;
private Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main4);
edittext = (EditText) findViewById(R.id.editText);
send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = edittext.getText().toString();
MyFragment5 fragment5 = new MyFragment5();
Bundle bundle = new Bundle();
bundle.putString("name", text);
fragment5.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
beginTransaction.add(R.id.layout,fragment5,"fragment5");
beginTransaction.commit();
Toast.makeText(MainActivity3.this,"向Fragment发送数据"+text,Toast.LENGTH_SHORT).show();
}
});
}
}
MyFragment5
package com.example.martin.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by Martin on 2016/1/11.
*/
public class MyFragment5 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2,container,false);
TextView tv = (TextView) view.findViewById(R.id.text);
String text = getArguments().get("name")+"";
tv.setText(text);
Toast.makeText(getActivity(),"已成功接收到"+text,Toast.LENGTH_SHORT).show();
return view;
}
}
2016年1月12日
----------------------------------------------------