安卓startActivityForResult實現多個Activity之間不同跳轉流程
阿新 • • 發佈:2019-02-04
問題描述:需要從MainActivity跳轉到B,再從B跳轉到C,最後從C跳回到MainActivity
先定義activity_main.xml,如下:
<span style="font-family:Microsoft YaHei;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/a_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px" android:textSize="22sp" android:text="I am activity A ..."/> <Button android:id="@+id/a_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/a_text" android:layout_margin="5px" android:text="Click Me!"/> </RelativeLayout> </span>
b.xml檔案,如下:
<span style="font-family:Microsoft YaHei;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/b_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px" android:textSize="22sp" android:text="I am activity B ..."/> <Button android:id="@+id/b_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5px" android:text="Click Me!"/> </LinearLayout> </span>
c.xml檔案,如下:
<span style="font-family:Microsoft YaHei;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/c_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px" android:textSize="22sp" android:text="I am activity C ..."/> <Button android:id="@+id/c_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5px" android:text="Click Me!"/> </LinearLayout> </span>
MainActivity如下:
<span style="font-family:Microsoft YaHei;">package com.example.activitytest;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button abutton ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
abutton = (Button)findViewById(R.id.a_button);
abutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
<span style="color:#ff0000;">Intent intent=new Intent();
intent.setClass(MainActivity.this, B.class);
startActivityForResult(intent, 1);</span>
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
</span>
B如下:
<span style="font-family:Microsoft YaHei;">package com.example.activitytest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class B extends Activity {
private Button bbutton ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
bbutton = (Button)findViewById(R.id.b_button);
bbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
<span style="color:#ff0000;">Intent intent=new Intent();
intent.setClass(B.this, C.class);
startActivityForResult(intent, 1);</span>
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
<span style="color:#ff0000;">protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1&& resultCode == 10){
this.setResult(10);
this.finish();
}
}</span>
}
</span>
C如下:
<span style="font-family:Microsoft YaHei;">package com.example.activitytest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class C extends Activity {
private Button cbutton ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.c);
cbutton = (Button)findViewById(R.id.c_button);
cbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
<span style="color:#ff0000;">C.this.setResult(10);
C.this.finish();</span>
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
</span>