I have 3 activities: 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.
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:
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.
Override onBackPressed()
in Activity
C
@Override
public void onBackPressed() {
finishAffinity();
}
This will cause all Activity
s running in the same task to finish. If the user starts the app again by tapping on it in the recent tasks window, Activity
A will be shown.
Start B
activity with startActivityForResult()
:
Intent intent = new Intent(A.this, B.class);
startActivityForResult(intent, 100);
and override in class 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
:
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. try to start Activity C like this. Intent intent = new Intent(this,ActivityC.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity();
If I understood you right, You could achieve this is using SharedPreferences:
In Your Activity A add this:
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:
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:
@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 .
@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 :
Activity 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 :
@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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.