[英]Deleting back stack of activities
I have 3 activities: A->B->C 我有3个活动:A-> B-> C
The user starts from activity A. When going to B he can press back and return to A. But... When user goes to C from B. I wish that A and B will be removed and user will exit the app when clicking back. 用户从活动A开始。转到B时,他可以按回去,然后返回到A。但是...当用户从B转到C时,我希望删除A和B,并在单击时退出应用程序。
I tried: 我试过了:
Intent intent = new Intent(B.this, C.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
but on back it still goes to B. If doing so: 但在背面仍然会转到B。如果这样做:
Intent intent = new Intent(VerifyPhoneActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
The user goes back to A, so.. It is not what I need. 用户返回到A,所以..这不是我所需要的。
Override onBackPressed()
in Activity
C 覆盖Activity
C中的onBackPressed()
@Override
public void onBackPressed() {
finishAffinity();
}
This will cause all Activity
s running in the same task to finish. 这将导致在同一任务中运行的所有Activity
完成。 If the user starts the app again by tapping on it in the recent tasks window, Activity
A will be shown. 如果用户在最近的任务窗口中通过点击再次启动应用程序,则将显示Activity
A。
Start B
activity with startActivityForResult()
: 使用startActivityForResult()
启动B
活动:
Intent intent = new Intent(A.this, B.class);
startActivityForResult(intent, 100);
and override in class A
startActivityForResult()
: 并在类A
startActivityForResult()
重写:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 100) {
if (resultCode == 1) {
finish();
}
}
}
Now in B
class set the result to be sent back to activity A
: 现在在B
类中,将结果设置回活动A
:
Intent intent = new Intent(VerifyPhoneActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
setResult(1);
finish();
100% working. 100%工作。 try to start Activity C like this. 尝试像这样启动活动C。 Intent intent = new Intent(this,ActivityC.class); Intent intent = new Intent(this,ActivityC.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(); startActivity();
If I understood you right, You could achieve this is using SharedPreferences: 如果我理解正确,则可以使用SharedPreferences实现此目的:
In Your Activity A add this: 在您的活动A中添加以下内容:
public class ActivityA extends AppCompatActivity {
SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
if (savedInstanceState == null){
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putBoolean("CloseApp", false);
editor.commit();
}
}
@Override
protected void onResume() {
super.onResume();
boolean finish = prefs.getBoolean("CloseApp", false);
if (finish){
this.finish();
}
}
In Activity B: 在活动B中:
public class ActivityB extends AppCompatActivity {
SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
}
@Override
protected void onResume() {
super.onResume();
boolean finish = prefs.getBoolean("CloseApp", false);
if (finish){
this.finish();
}
}
And Activity C: 活动C:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putBoolean("CloseApp", true);
editor.commit();
}
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
That should work. 那应该工作。
you can just write this in Activity C if your application work from JELLY_BEAN to up . 如果您的应用程序从JELLY_BEAN开始运行,则只需在Activity C中编写即可。
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onBackPressed() {
super.onBackPressed();
finishAffinity();
}
if your app work under JELLY_BEAN you can use this solution : 如果您的应用程序在JELLY_BEAN下工作,则可以使用以下解决方案:
Activity C : 活动C:
@Override
public void onBackPressed() {
Intent i = new Intent(C.this,A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("BackFromC",true);
startActivity(i);
super.onBackPressed();
}
Activity A in onCreate : onCreate中的活动A:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
if(getIntent() != null){
if(getIntent().getBooleanExtra("BackFromC",false)){
finish();
}
}
... your Code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.