設定好要轉跳的Activity, 然後利用startActivity或者startActivityForResult來執行。
先寫原始的頁面,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:hint="hello"
android:id="@+id/result_text"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="startActivityForResult"
android:id="@+id/start_activity_for_result"
/>
</LinearLayout>
一個TextView用來呈現回來的字串 Button會轉跳到另外一隻Activity
然後再寫一個另外一個Activity的頁面。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/other_activtity_edittext"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="startActivityForResult back"
android:id="@+id/start_activity_for_result_back"
/>
</LinearLayout>
這邊有一個輸入框可以讓你輸入要回傳的文字public class MainActivity extends AppCompatActivity {
private Button startActivtyForResult;
private TextView resultText;
public static final int resultNum = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
startActivtyForResult = (Button) findViewById(R.id.start_activity_for_result);
resultText = (TextView) findViewById(R.id.result_text);
startActivtyForResult.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, OtherActivity.class);
startActivityForResult(intent, resultNum);
}
});
}
}
當Button按下去就會轉跳到下一個Activity定義第二個方法, 當按下去的時候,
第二個按鈕會利用startActivityForActivity轉跳到另外一個Activity。
public class OtherActivity extends AppCompatActivity {
private EditText otherActivityEditText;
private Button startActivityForResultBack;
public static final String FLAG = "BACK_STRING";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
otherActivityEditText = (EditText) findViewById(R.id.other_activtity_edittext);
startActivityForResultBack = (Button) findViewById(R.id.start_activity_for_result_back);
startActivityForResultBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
Bundle b = new Bundle();
b.putString(FLAG, otherActivityEditText.getText().toString());
intent.putExtras(b);
OtherActivity.this.setResult(RESULT_OK, intent);
finish();
}
});
}
}
當另外一個Activity傳送回來的時候, 它會接收由startActivityForResult傳送回來的資訊。@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
if(requestCode == resultNum){
resultText.setText(data.getExtras().getString(OtherActivity.FLAG));
}
}
}
當使用startActivityForResult的時候, A Activity狀態長這樣onCreat()-> onStart()-> onResume()-> 按下到下一個Activity Button
-> onPause() -> onStop()-> 按回到A Activity Button
-> onRestart() -> onStart() -> onResume()
沒有留言:
張貼留言