我有两个活动:A和B.
从A到B:
onCreate
Context context = this;
的onClick
Intent i = new Intent(context,B.class);
startActivity(i);
从B到A: (如果B有一个按钮返回A)
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
finish();
}
});
但如果流程如下:
呼叫B,正在观看B的用户单击主页按钮并再次单击已启动活动中的B并单击该按钮以调用finish(),它将转到主页而不是我的第一个活动A.我怎么能这样做?
编辑:
流
答案 0 :(得分:1)
为什么使用getApplicationContext()
代替使用this
或Activity A name.
A - > B
Intent i = new Intent(yourcurrentActivity,B.class);
startActivty(i);
B - > A
致电finish()
更新如果您使用此功能: - 将其删除
android:noHistory="true";
答案 1 :(得分:1)
试试这个:
startActivity(new Intent(B.this,B.class));
并在活动A中完成()
// in your activity A
Intent i = new Intent(getApplicationContext(),B.class);
startActivity(i);
finish();
和
public void OnBackPressed()
{
finish();
}
答案 2 :(得分:0)
致电
onBackPressed();
而不是
finish();
答案 3 :(得分:0)
试试这个:
public void startActivityForResult (Intent intent, int requestCode)
答案 4 :(得分:0)
检查下面的代码:我有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_activity:
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_activity:
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