Acticvity第一次启动的时候会被调用
Activity正在变为可见
Activity已可见,并获取焦点
一个Activity位于另一个Activity之上,但是不完全消失
Activity不可见
退出了整个Activity
Activity被停止后重新启动
oncreate---onstart---onresume
onpause---onstop
onrestart---onstart---onresume
ondestory
对SecondActivity进行创建,同时需要在layout文件下进行布局的设置
public class SecondActivity extends AppCompatActivity {
private TextView text;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linear);
text = (TextView) findViewById(R.id.textview_id);
Log.i("SecondActivity","text = "+text.getText().toString());
}
}
step2:
Androidmainfest.xml中对新增的activity进行声明
step3:
在MainActivity中对实现跳转
Intent intent = new Intent(MainActivity.this , SecondActivity.class);
startActivity(intent);
step1:
在MainActivity定义intent对象
Intent intent = new Intent();
intent.setAction("com.example.myfirstapplication.ACTION_TEST");
intent.addCategory("aaa");
Uri uri = Uri.parse("baidu://www.baidu.com/nihaonihao");
intent.setData(uri);
startActivity(intent);
step2:
创建需要跳转的Activity
public class ThirdActivity extends AppCompatActivity {
private static String TAG = "ThirdActivity";
private TextView text;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
text = (TextView) findViewById(R.id.textView_third);
Log.i(TAG,"text = "+text.getText().toString());
}
}
step3:
在AndroidMainfest.xml文件中进行Activity的声明
通常情况下,我们启动一个Activity是调用startActivity方案,此方法调用后是不会有结果返回的,那么如果需要另外一个Activity有结果可以返回怎么处理,需要调用startActivityForResult方法
public void startActivityForResult (Intent intent,
int requestCode)
intent
表示需要启动的intent对象
requestCode
当Activity结束时requestCode将归还在onActivityResult()中。以便确定返回的数据是从哪个Activity中返回,用来标识目标activity,请求访问界面即主动方的int型标志
异常
android.content.ActivityNotFoundException
onActivityResult接收回传数据
protected void onActivityResult (int requestCode,
int resultCode,
Intent data)
requestCode
int: requestCode用于与startActivityForResult中的requestCode中值进行比较判断,是以便确认返回的数据是从哪个Activity返回的。
resultCode
int: resultCode是由子Activity通过其setResult()方法返回。适用于多个activity都返回数据时,来标识到底是哪一个activity返回的值
data
Intent: 可以通过data.getXxxExtra( );方法来获取指定数据类型的数据
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case 1:
if(resultCode == RESULT_OK)
{
String returndata = data.getStringExtra("data_return");
Log.i(TAG,"returndata = "+returndata);
}
break;
default:
}
}
返回结果给调用者
public final void setResult (int resultCode,
Intent data)
resultCode
int:当Activity结束时resultCode将归还在onActivityResult()中,一般为RESULT_CANCELED , RESULT_OK该值默认为-1
Intent
回传给Activity
Intent result_intent = new Intent();
result_intent.putExtra("data_return","hello MainActivity");
setResult(RESULT_OK,result_intent);
finish();
默认为-1
Intent
回传给Activity
Intent result_intent = new Intent();
result_intent.putExtra("data_return","hello MainActivity");
setResult(RESULT_OK,result_intent);
finish();