1.启动一个activity并从它中拿到回执的值
通过startActivityForResult启动activity,从onActivityResult拿到返回值
代码示例
public class FirstActivity extends AppCompatActivity {
private static final int REQUESTCODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
findViewById(R.id.startIntent).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivityForResult(intent, REQUESTCODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUESTCODE:
if (resultCode == RESULT_OK) {
String returnData = data.getStringExtra("RETURN_DATA");
Toast.makeText(this, "return data: " + returnData, Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
}
通过setResult向上一个活动返回数据
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
findViewById(R.id.text).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("RETURN_DATA", "返回数据给第一页");
setResult(RESULT_OK, intent);
finish();
}
});
}
}
2.活动被回收了的处理
代码示例
public class FirstActivity extends AppCompatActivity {
private String tempData = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
tempData = "内存回收之前需要保存的测试数据";
if (savedInstanceState != null) {
tempData = savedInstanceState.getString("tempdata");
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("tempdata", tempData);
}
}
3.启动模式
在清单文件中添加 android:launchMode
- standard
- singleTop
- singleTask
- singleInstance
standard
系统不在乎是否已经在返回栈总存在,每次启动都会重新创建一个实例
singleTop
启动活动时,如果返回栈顶已经是该活动,则不在重新创建新的活动。如果不在栈顶,则仍然会再次创建该活动。
singleTask
系统会在返回栈中检查是否存在该活动,如果存在,则直接使用它,并把这个活动之上的其他活动统统出栈。如不存在,则重新创建。
singleInstance
启用一个新的返回栈来管理这个活动。如果有上个活动A(standard)->B(singleInstance)->C(standard)依次点击跳转完,点击back键回退时,回退顺序为C->A->B。因为AC为同一个栈,C出栈后自然会显示A,A出栈后才会显示另一个栈B。
4.BaseActivity,ActivityCollector管理启动的activity
代码示例
public class ActivityCollector {
private static List activities = new ArrayList<>();
public static void addActivity(Activity activity) {
activities.add(activity);
}
public static void removeActivity(Activity activity) {
activities.remove(activity);
}
/**
* 退出程序
*/
public static void finishAll() {
for (Activity activity : activities) {
if (!activity.isFinishing()) {
activity.finish();
}
}
}
/**
* 保证程序完全退出,杀掉进程,只能用于杀掉当前程序进程
*/
public static void killProcess (){
android.os.Process.killProcess( android.os.Process.myPid());
}
}
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//打印当前活动的名称
Log.d("BaseActivity", "Current Activity Name: " + getClass().getSimpleName());
ActivityCollector.addActivity(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.removeActivity(this);
}
}
5.启动activity较优方式
代码示例
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
public static void startSecondActivity(Context context, String param1, String param2) {
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("param1", param1);
intent.putExtra("param2", param2);
context.startActivity(intent);
}
}