检查下面的代码:我有 3 个活动:MainActivity,A_Acvitiy,B_actiivty(希望它按照您的要求工作..)
package com.example.androidtest;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void callActivity(View v) {
if (v.getId() == R.id.button1) {
// A
startActivity(new Intent(MainActivity.this, A_activity.class));
}
if (v.getId() == R.id.button2) {
// B
startActivity(new Intent(MainActivity.this, B_activity.class));
if (GeneraClass.isFormB) {
GeneraClass.isFormB = false;
finish();
}
}
}
}
A_活动:
package com.example.androidtest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class A_activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.a_layout);
}
public void callActivity(View v) {
if (v.getId() == R.id.button1) {
// B
startActivity(new Intent(A_activity.this, B_activity.class));
finish();
}
}
}
b_活动:
package com.example.androidtest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class B_activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.b_layout);
}
public void callActivity(View v) {
if (v.getId() == R.id.home) {
// home
GeneraClass.isFormB = true;
startActivity(new Intent(B_activity.this, MainActivity.class));
finish();
}
}
}
我的额外课程:
package com.example.androidtest;
public class GeneraClass {
public static boolean isFormB = false;
}
所以,Home-->A(finish)-->B--->Home(if from b then finish)-->B-->Back--> Home --->Back--> exit App