四大组建-Activity
2018-09-30 本文已影响0人
it奔跑在路上
1.Activity的跳转
显示跳转
方法一:
Intent intent=new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
方法二:
Intent intent2=new Intent();
intent2.setClass(本类,另外将要跳转的类); (intent2.setClass(MainActivity.this,SecondActivity.class);)
startActivity(intent2);
方法三:
Intent intent2=new Intent();
intent2.setClassName(this,“将要跳转界面的名字”);
startActivity(intent2);
方法四:
Intent intent2=new Intent();
intent2.setComponent(new ComponentName(MainActivity.this, OtherActivity.class));
startActivity(intent2);
隐式跳转
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("Zyptest");
startActivity(intent);
}
});
}
}
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="Zyptest"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
更多隐式跳转
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http:www.baidu.com"));
startActivity(intent);
}
});
}
}
Activity回调
三:跳转到另一个Activity后,当返回时能返回数据
1.在跳转的Activity端,调用startActivityForResult(intent2, 2),跳转到下一个Activity,其中第一个参数为传入的意图对象,第二个为设置的请求码;
2.跳转到第二个Activity后,调用setResult(10, intent)方法可返回上一个Activity,其中第一个参数为结果码,第二个为传入的意图对象;
3.在第一个Activity通过onActivityResult()方法获得返回的数据。
public class MainActivity extends AppCompatActivity {
private int requestCode = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent,requestCode);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == 4){
String nihao = data.getStringExtra("nihao");
Log.v("MainActivity", nihao);
}
}
}
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button bt2 = (Button) findViewById(R.id.bt2);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
intent.putExtra("nihao", "我是SecondActivity");
setResult(4,intent);
finish();
}
});
}
}