Activity生命周期5状态:启动、运行、暂停、停止、销毁。
Fragment 的生命周期比 Activity 的生命周期多了以下几个方法,具体如下。
onAttach():Fragment和Activity 建立关联时调用。
onCreateView():Fragment创建视图(加载布局)时调用。
onActivityCreate():Fragment相关联的Activity 已经创建完成时调用。
onDestroyView():Fragment关联的视图被移除时调用。
onDetach():Fragment和Activity 解除关联时调用。
启动模式:
standard
singleTop (在栈顶才复用) 适合开启渠道多的,避免重复创建
singleTask 先查找是否存在,把其他的全部弹出 主界面、耗费资源的界面
singleInstance 任务栈 来电界面
AndroidManifest.xml中注册Activity
<activityandroid:name=".MainActivity"android:exported="true"></application>
启动和关闭activity
@Overridepublic void onClick(View view) {Intent intent =new Intent(MainActivity.this, jump.class);startActivity(intent);}
Button btn2 = findViewById(R.id.btn2);btn2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {finish();}});}
向下一个发送数据
①无bundle
发数据
public void onClick(View view) {String acc =id.getText().toString();String psd =psw.getText().toString();Intent intent =new Intent(MainActivity.this, jump.class);intent.putExtra("username",acc);intent.putExtra("psw",psd);startActivity(intent);}
接收
public class jump extends AppCompatActivity {@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.jump);Button btn2 = findViewById(R.id.btn2);Intent intent = this.getIntent();String username= intent.getStringExtra("username");String psw = intent.getStringExtra("psw");TextView tv1 = findViewById(R.id.tv1);TextView tv2 = findViewById(R.id.tv2);tv1.setText(username);tv2.setText(psw);btn2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {finish();}});}
}
②带bundle
@Overridepublic void onClick(View view) {String acc =id.getText().toString();String psd =psw.getText().toString();Bundle bundle = new Bundle();bundle.putString("username",acc);bundle.putString("psw",psd);Intent intent =new Intent(MainActivity.this, jump.class);intent.putExtras(bundle);startActivity(intent);}
Button btn2 = findViewById(R.id.btn2);Bundle bundle = this.getIntent().getExtras();String username= bundle.getString("username");String psw = bundle.getString("psw");
创建fragment
public class FR extends Fragment {@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View v = inflater.inflate(R.layout.activity_main,container,false);return v;}
}